Android自定义View之刮刮卡效果



Android自定义View之刮刮卡效果

已封装成jar包,需要的可以自行下载 http://download.csdn.net/detail/u010670151/9490352,且按照自己的想法开放了部分方法如:设置刮刮卡背景图片(隐藏的内容)以及设置刮卡时的画笔大小。画笔的样式及刮刮卡的前景色没有开放出来,有需要的话可以自行更改。

1GGCard类实现:

@SuppressLint("DrawAllocation")
public class GGCard extends View {
        /**
         * @param mPaint
         *           擦除前景色的画笔
         */
        PaintmPaint;
        /**
         * @param mCanvas
         *           用于绘制刮刮卡的前景色(bitmap)
         *
         */
        CanvasmCanvas;
        /**
         *记录Touch事件中相对与View本身的上次坐标
         */
        intmX, mY;
        /**
         * @param mPath
         *           保存画笔路径
         */
        PathmPath;
        /**
         * @param ForegroundBitmap
         *           刮刮卡的前景色
         * @param BackgroundBitmap
         *           刮刮卡的背景色
         */
        BitmapForegroundBitmap, BackgroundBitmap;
 
        publicGGCard(Context context, AttributeSet attrs) {
                  super(context,attrs);
                  mPaint= new Paint();
                  mPath= new Path();
                  mPaint.setAntiAlias(true);
                  mPaint.setDither(true);
                  mPaint.setStyle(Paint.Style.STROKE);
                  mPaint.setStrokeJoin(Paint.Join.ROUND);//圆角
                  mPaint.setStrokeCap(Paint.Cap.ROUND);//圆角
                  mPaint.setColor(Color.RED);
                  mPaint.setStrokeWidth(10);
        }
 
        publicvoid setPaintStrokeWidth(int strokeWidth) {
                  mPaint.setStrokeWidth(strokeWidth);
        }
 
        publicvoid setBackgroundPicture(Bitmap bitmap) {
                  BackgroundBitmap= bitmap;
        }
 
        @Override
        protectedvoid onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
                  super.onMeasure(widthMeasureSpec,heightMeasureSpec);
                  ForegroundBitmap= Bitmap.createBitmap(getMeasuredWidth(),
                                    getMeasuredHeight(),Config.ARGB_8888);
                  mCanvas= new Canvas(ForegroundBitmap);
                  mCanvas.drawColor(Color.parseColor("#e3e3e3"));
        }
 
        @Override
        protectedvoid onDraw(Canvas canvas) {
                  super.onDraw(canvas);
                canvas.drawBitmap(ForegroundBitmap, 0,0, null);
                  if(BackgroundBitmap != null) {
                           canvas.drawBitmap(BackgroundBitmap,
                                              (getWidth()- BackgroundBitmap.getWidth()) / 2,
                                              (getHeight()- BackgroundBitmap.getHeight()) / 2, null);
                  }
                  mPaint.setXfermode(newPorterDuffXfermode(PorterDuff.Mode.DST_OUT));
                  mCanvas.drawPath(mPath,mPaint);
                  canvas.drawBitmap(ForegroundBitmap,0, 0, null);
        }
 
        @SuppressLint("ClickableViewAccessibility")
        @Override
        publicboolean onTouchEvent(MotionEvent event) {
                  intnX = (int) event.getX();
                  intnY = (int) event.getY();
                  switch(event.getAction()) {
                  caseMotionEvent.ACTION_DOWN:
                           mX= nX;
                           mY= nY;
                           mPath.moveTo(mX,mY);
                           break;
                  caseMotionEvent.ACTION_MOVE:
                           mPath.lineTo(nX,nY);
                           mX= nX;
                           mY= nY;
                           break;
                  caseMotionEvent.ACTION_UP:
                           break;
                  }
                  invalidate();
                  returntrue;
        }
 
}


2XML中引用:

<com.sun.view.GGCard
           android:layout_width="match_parent"
           android:layout_height="180dp"
           android:id="@+id/ggCard"
           />


3Java代码中使用:

GGCard ggCard=(GGCard) findViewById(R.id.ggCard);
ggCard.setBackgroundPicture(BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher));
ggCard.setPaintStrokeWidth(20);


4运行效果如下:

 Android自定义View之刮刮卡效果_第1张图片 Android自定义View之刮刮卡效果_第2张图片

注意:上面列举的实例Bitmap对象未经优化,使用时注意优化。

你可能感兴趣的:(android,自定义view,刮刮卡)