自定义View
public class MyPath extends View {
private int width;
private int height;
private Paint mPaint;
private Path path;
private Paint mPaintPoint;
private int countX=80;
private Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what){
case 0x23:
countX--;
invalidate();
if (countX<0){
countX=80;
handler.sendEmptyMessageDelayed(0x23,1000);
}
break;
}
}
};
public MyPath(Context context) {
super(context);
}
public MyPath(Context context, AttributeSet attrs) {
super(context, attrs);
mPaint=new Paint();
mPaint.setAntiAlias(true);
mPaint.setColor(Color.WHITE);
mPaint.setStyle(Paint.Style.FILL);
mPaintPoint=new Paint();
mPaintPoint.setColor(Color.GREEN);
mPaintPoint.setStrokeWidth(5);
path=new Path();
PorterDuffXfermode mode=new PorterDuffXfermode(PorterDuff.Mode.SRC_IN);
mPaintPoint.setXfermode(mode);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
width=getDefaultSize(getSuggestedMinimumWidth(),widthMeasureSpec);
height=getDefaultSize(getSuggestedMinimumHeight(),heightMeasureSpec);
setMeasuredDimension(width,height);
mBitMap=Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_8888);
mCanvas=new Canvas(mBitMap);
}
private Canvas mCanvas;
private Bitmap mBitMap;
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawColor(Color.BLUE);
mCanvas.drawCircle(300,300,150,mPaint);
handler.sendEmptyMessage(0x23);
path.reset();
path.moveTo(500,200);
path.lineTo(500,500);
path.lineTo(countX, 500);
path.lineTo(countX,200);
for (int i = 0; i <20; i++) {
path.rQuadTo(20,20,40,0);
path.rQuadTo(20,-20,40,0);
}
path.close();
mCanvas.drawPath(path,mPaintPoint);
canvas.drawBitmap(mBitMap,0,0,null);
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:myview="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" tools:context=".MainActivity">
<com.lingzhuo.test.mypath.MyPath
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>