activity_main.xml</br>
<pre><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
tools:context=".MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="btnTeaching"
android:text="Teaching" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="btnTopBar"
android:text="TopBar" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="btnMyTextView"
android:text="MyTextView" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="btnShineTextView"
android:text="ShineTextView" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="btnCircleProgress"
android:text="CircleProgress" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="btnVolumeView"
android:text="VolumeView" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="btnMyScrollView"
android:text="MyScrollView" />
</LinearLayout>
</pre>
MainActiviy</br>
<pre>
package com.imooc.systemwidget;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity {
private Intent mIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mIntent = new Intent(this, MyViewTest.class);
}
public void btnTeaching(View view) {
mIntent.putExtra("flag", 0);
startActivity(mIntent);
}
public void btnMyTextView(View view) {
mIntent.putExtra("flag", 1);
startActivity(mIntent);
}
public void btnShineTextView(View view) {
mIntent.putExtra("flag", 2);
startActivity(mIntent);
}
public void btnCircleProgress(View view) {
mIntent.putExtra("flag", 3);
startActivity(mIntent);
}
public void btnVolumeView(View view) {
mIntent.putExtra("flag", 4);
startActivity(mIntent);
}
public void btnMyScrollView(View view) {
mIntent.putExtra("flag", 5);
startActivity(mIntent);
}
public void btnTopBar(View view) {
startActivity(new Intent(this, TopBarTest.class));
}
}
</pre>
MyViewTest</br>
<pre>
package com.imooc.systemwidget;
import android.app.Activity;
import android.os.Bundle;
public class MyViewTest extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int flag = getIntent().getIntExtra("flag", -1);
switch (flag) {
case 0:
setContentView(R.layout.teaching);
break;
case 1:
setContentView(R.layout.my_textview);
break;
case 2:
setContentView(R.layout.shine_textview);
break;
case 3:
setContentView(R.layout.circle_progress);
CircleProgressView circle = (CircleProgressView) findViewById(R.id.circle);
circle.setSweepValue(0);
break;
case 4:
setContentView(R.layout.volume);
break;
case 5:
setContentView(R.layout.my_scrollview);
break;
}
}
}
</pre>
1、Teaching
</br>
1.1、teaching.xml
<pre>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.imooc.systemwidget.TeachingView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</pre>
1.2、TeachingView
<pre>package com.imooc.systemwidget;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
public class TeachingView extends View {
public TeachingView(Context context) {
super(context);
}
public TeachingView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TeachingView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
//查看super.onMeasure方法,可以发现,系统
//最终会调用setMeasuredDimension(int measuredWidth,intmeasuredHeight())方法
//将测量后的宽高值设置进去,从而完成测量工作,这里重写onMeasure()方法后,最终要
//做的工作就是把测量后的宽高值作为参数设置给setMeasureDimension()方法。
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(
measureWidth(widthMeasureSpec),
measureHeight(heightMeasureSpec));
}
//通过判断测量的模式,给出不同的测量值,这里求宽度
private int measureWidth(int measureSpec) {
int result = 0;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
if (specMode == MeasureSpec.EXACTLY) {
result = specSize;
} else {
result = 200;
if (specMode == MeasureSpec.AT_MOST) {
//Math.min() 方法是通过三元运算符实现的,这里求最小值。
result = Math.min(result, specSize);
}
}
return result;
}
//通过判断测量的模式,给出不同的测量值,这里求高度
private int measureHeight(int measureSpec) {
int result = 0;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
if (specMode == MeasureSpec.EXACTLY) {
result = specSize;
} else {
result = 200;
if (specMode == MeasureSpec.AT_MOST) {
result = Math.min(result, specSize);
}
}
return result;
}
@Override
protected void onDraw(Canvas canvas) {
//执行整个操作来完成你的绘图
super.onDraw(canvas);
//填充整个画布的位图,Color.GRAY是灰色
canvas.drawColor(Color.GRAY);
int width = getWidth();
int height = getHeight();
Log.d("xys", "width : " + width + " height : " + height);
}
}
</pre>
</br>
2、MyTextView
</br>
2.1、my_textview.xml
<pre>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.imooc.systemwidget.MyTextView
android:layout_width="200dp"
android:layout_height="50dp"
android:gravity="center"
android:text="Android"
android:textSize="30sp" />
</LinearLayout>
</pre></br>
2.2、MyTextView
<pre>
package com.imooc.systemwidget;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.TextView;
public class MyTextView extends TextView {
private Paint mPaint1, mPaint2;
public MyTextView(Context context) {
super(context);
initView();
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
initView();
}
public MyTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView();
}
private void initView() {
//创建带有默认设置的新绘图
mPaint1 = new Paint();
//天蓝色
mPaint1.setColor(getResources().getColor(android.R.color.holo_blue_light));
mPaint1.setStyle(Paint.Style.FILL);
mPaint2 = new Paint();
//黄色
mPaint2.setColor(Color.YELLOW);
mPaint2.setStyle(Paint.Style.FILL);
}
@Override
protected void onDraw(Canvas canvas) {
// 绘制外层矩形,使用指定的油漆绘制指定的矩形。矩形将根据颜料的风格填充或镶框。
canvas.drawRect(
//绘制矩形的左边
0,
//绘制矩形的顶部
0,
//绘制矩形的右边,获取这个视图的原始宽度。
getMeasuredWidth(),
//绘制矩形的底部
getMeasuredHeight(),
//用来绘制矩形的油漆
mPaint1);
// 绘制内层矩形
canvas.drawRect(
10,
10,
getMeasuredWidth() - 10,
getMeasuredHeight() - 10,
mPaint2);
//将当前的矩阵和剪辑保存到一个私有堆栈中。
//随后调用转换、缩放、旋转、倾斜、concat或clipRect,clipPath将照常运行,
//但是当恢复平衡调用时,这些调用将被忘记,
//并且在save()之前存在的设置将被恢复。
canvas.save();
// 绘制文字前向右平移150像素,向下平移50像素。
canvas.translate(150, 50);
// 父类完成的方法,即绘制文本
super.onDraw(canvas);
//这个调用平衡了之前的save()调用,
//并且用于删除自上次保存调用以来对矩阵/ clip状态的所有修改。
//调用恢复()的次数比调用save()的次数要多。
canvas.restore();
}
}
</pre>