Android自定义控件:刮刮乐控件的简单实现

 多话不说,直接上代码:

package com.xuganwen.colorfullimage;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ImageView;

import com.xuganwen.colorfullphoto.R;

public class MyImageView extends ImageView {

    private Paint paint;
    private Path mPath;
    private Bitmap mBgBitmap;
    private Bitmap mFgBitmap;
    private Canvas mCanvas;
    private Paint paint2;

    public MyImageView(Context context) {
        super(context);
    }

    public MyImageView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MyImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private void init() {
        paint = new Paint();
        paint.setAlpha(0);
        paint.setAntiAlias(true);
        paint.setStrokeJoin(Paint.Join.ROUND);
        paint.setStrokeCap(Paint.Cap.ROUND);
        paint.setStrokeWidth(50);
        paint.setStyle(Paint.Style.STROKE);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));


        paint2 = new Paint();
//        paint.setAlpha(0);
        paint2.setAntiAlias(true);
        paint2.setStrokeJoin(Paint.Join.ROUND);
        paint2.setStrokeCap(Paint.Cap.ROUND);
        paint2.setStrokeWidth(2);
        paint2.setTextSize(60);
        paint2.setStyle(Paint.Style.STROKE);
        paint2.setTextAlign(Paint.Align.CENTER);


        mPath = new Path();

        mBgBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.a);
        mFgBitmap = Bitmap.createBitmap(mBgBitmap.getWidth(), mBgBitmap.getHeight(), Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(mFgBitmap);
        mCanvas.drawColor(Color.GRAY);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mPath.reset();
                mPath.moveTo(event.getX(), event.getY());
                break;
            case MotionEvent.ACTION_MOVE:
                mPath.lineTo(event.getX(), event.getY());
                break;
            case MotionEvent.ACTION_UP:
                break;
        }
        mCanvas.drawPath(mPath, paint);
        invalidate();
        return true;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
//        canvas.drawBitmap(mBgBitmap, 0, 0, null);   如果图片  这种方式可行
//        setImageBitmap(mBgBitmap);   如果是图片,这种方式也可行
        canvas.drawText("xuganwen",getWidth()/2,getHeight()/2,paint2);  //如果是文字,可以选择这种方式
        canvas.drawBitmap(mFgBitmap, 0, 0,null );
    }
}

布局代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <com.xuganwen.colorfullimage.MyImageView
        android:layout_width="200dp"
        android:layout_height="200dp"
        />
</RelativeLayout>

可以直接copy到项目中使用,如果要定制刮刮乐文字,可以自己提供对外settext()接口。

图我就不上了。。。

猜你喜欢

转载自blog.csdn.net/always_myhometown/article/details/81118088