1. 程式人生 > >Android 實現簡單的刮刮樂

Android 實現簡單的刮刮樂

上次突發奇想想做個不一樣的刮刮樂,參考了大神們的程式碼。然後,寫出了這種效果。
效果圖

1,先繼承TextView

/**
 * Created by Administrator on 2017/2/25.
 * 刮刮樂 效果
 */

public class ScratchTextView  extends TextView{
    private static final String TAG = "ScratchTextView";
    private Bitmap mbitmap;// 蓋在字上面的圖片
    private Canvas mCanvas; //畫線的畫布
    private
Paint mPaint;//劃線的畫筆 private Path mPath;//線 private float mX, mY; private float TOUCH_TOLERANCE; private boolean isInited = false;//用於判斷時候覆蓋了textview的文字 public ScratchTextView(Context context) { super(context); } public ScratchTextView(Context context, AttributeSet attrs) { super
(context, attrs); } public ScratchTextView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } //onDraw初始化的時候呼叫一次,然invalidate()的時候呼叫, @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (isInited) { mCanvas.drawPath(mPath, mPaint);//把線畫到mCanvas上,mCanva會把線畫到mBitmap
canvas.drawBitmap(mbitmap, 0, 0, null);// 把mBitmap畫到textview上 canvas是父textvie傳過來的。 } } /** * 初始化刮刮卡 * * @param bgColor 刮刮卡背景色,用於蓋住下面的字 * @param paintStrokeWidth 擦除線寬 * @param touchTolerance 畫線容差 */ public void initScratchCard(final int bgColor, final int paintStrokeWidth, float touchTolerance) { TOUCH_TOLERANCE = touchTolerance; mPaint = new Paint();//建立畫筆 mPaint.setAlpha(240); mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));// mPaint.setAntiAlias(true);// 抗鋸齒 mPaint.setDither(true);// 防抖動 mPaint.setStyle(Paint.Style.STROKE);// 畫筆型別: STROKE空心 FILL實心 FILL_AND_STROKE用契形填充 mPaint.setStrokeJoin(Paint.Join.ROUND);// 畫筆接洽點型別 mPaint.setStrokeCap(Paint.Cap.ROUND);// 畫筆筆刷型別 mPaint.setStrokeWidth(paintStrokeWidth);// 畫筆筆刷寬度 mPath = new Path(); //建立一個空的Bit mbitmap = Bitmap.createBitmap(getLayoutParams().width, getLayoutParams().height, Bitmap.Config.ARGB_8888); // mCanvas = new Canvas(mbitmap);//通過bitmap生成一個畫布 Paint paint = new Paint();//用於繪製生成的背景圖片的字型 paint.setTextSize(50);///字型大小 paint.setColor(Color.parseColor("#ff0717")); mCanvas.drawColor(bgColor);//背景色 //.從資原始檔中獲取圖片 Bitmap photo = BitmapFactory.decodeResource(this.getResources(), R.drawable.maojin_ic); mCanvas.drawBitmap(photo, 0 , 1 , paint);//設定遮蓋層的圖片。比如說,衣服 //設定外層的文字。 mCanvas.drawText("不要嘛!!", getLayoutParams().width / 4, getLayoutParams().height / 2 + 15, paint); isInited = true; } //該觸膜事件可以定義在呼叫的activity中實現 @Override public boolean onTouchEvent(MotionEvent event) { if (!isInited) { return true; } switch (event.getAction()) { case MotionEvent.ACTION_DOWN: mPath.reset(); // 重置繪製路線,即隱藏之前繪製的軌跡 mPath.moveTo(event.getX(), event.getY()); // mPath繪製的繪製起點 mX = event.getX(); mY = event.getY(); invalidate();//更新介面 Log.d(TAG, mX + "|" + mY); break; case MotionEvent.ACTION_MOVE: //x和y移動的距離 float dx = Math.abs(event.getX() - mX); float dy = Math.abs(event.getY() - mY); //x,y移動的距離大於畫線容差 if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) { // 二次貝塞爾,實現平滑曲線;mX, mY為操作點,(x + mX) / 2, (y + mY) / 2為終點 mPath.quadTo(mX, mY, (event.getX() + mX) / 2, (event.getY() + mY) / 2); // 第二次執行時,第一次結束呼叫的座標值將作為第二次呼叫的初始座標值 mX = event.getX(); mY = event.getY(); Log.d(TAG, mX + "|" + mY); invalidate(); break; } } return true; } }

2,如何使用

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shave);
        ButterKnife.bind(this);
        String[] str_reward = {"討厭!", "舒服1000!", "真爽10000!","真爽10000!"};
        scratchTv.setText(str_reward[getRandom()]);
        scratchTv.initScratchCard(0xFFFFFFFF, 20, 1f);//呼叫方法,初始化scartchTextView 最外層的顏色
//        getResources().getColor(R.color.btn_fuse_color_c)
    }
    private int getRandom() {
        Random random = new Random();
        int number = random.nextInt(5);
        return number;
    }
}