看了一个教程跟上做的,分享出来给大家看看直接上源码
MainActivity .java
public class MainActivity extends Activity {
guaguaView mgg ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mgg = (guaguaView) findViewById(R.id.guagua);
mgg.setOnGuaGuaCompleteListener(new OnGuaGuaComplete() {
@Override
public void complete() {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "您中了500w!!", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
activity_main.xml
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:orientation="horizontal">
<com.zx.guagua.view.guaguaView
android:id="@+id/guagua"
android:layout_width="200dp"
android:layout_height="150dp"
android:layout_gravity="center"
/>
</LinearLayout>
guaguaView.java
public class guaguaView extends View {
private Paint mOutterPain = null;
private Path mPath;//记录用户滑动的轨迹
private Canvas mCanvas;//画布
private Bitmap mBitmap;//图片
private int mLastX ;
private int mLastY;
//-----------------------------------//
private Bitmap bitmap;
//-----------------------------------//
private String mText;
private Paint mBackPaint;
private int textSize;
//判断只改遮盖层区域是否消除达到阈值
private volatile boolean mComplete = false;
/**
* 刮刮卡刮完调用的接口
*
* */
public interface OnGuaGuaComplete{
void complete();
}
private OnGuaGuaComplete mListener;
public void setOnGuaGuaCompleteListener(OnGuaGuaComplete mListener){
this.mListener = mListener;
}
/**
* 记录刮奖信息文本的宽和高
* */
private Rect mTextBound;
public guaguaView(Context context) {
this(context,null);
}
public guaguaView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
public guaguaView(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//获取页面的宽和高
int width = getMeasuredWidth();
int height = getMeasuredHeight();
//初始化我们的bitMap
mBitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
setOutPaint();
setUpBackPaint();
mCanvas.drawColor(Color.parseColor("#e0e0e0"));
}
/**
* 设置我们绘制获奖信息的画笔属性
*
* */
private void setUpBackPaint() {
mBackPaint.setColor(Color.BLACK);
mBackPaint.setStyle(Style.FILL);
mBackPaint.setTextSize(textSize);
//获得当前画笔绘制文本的宽和高
mBackPaint.getTextBounds(mText, 0, mText.length(), mTextBound);
}
/**
* 进行一些初始化操作
* */
private void init() {
mOutterPain = new Paint();
mPath = new Path();
bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.z12);
mText = "500万大奖!!";
mTextBound = new Rect();
mBackPaint = new Paint();
textSize = 30;
}
/**
* 设置绘制path画笔的一些属性
* */
private void setOutPaint(){
mOutterPain.setColor(Color.RED);
mOutterPain.setAntiAlias(true);
mOutterPain.setDither(true);
mOutterPain.setStrokeJoin(Paint.Join.ROUND);
mOutterPain.setStrokeCap(Paint.Cap.ROUND);
mOutterPain.setStyle(Style.STROKE);
mOutterPain.setStrokeWidth(20);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
int x = (int)event.getX();
int y = (int)event.getY();
switch (action) {
case MotionEvent.ACTION_DOWN://按下时绘制路径
mLastX = x;
mLastY = y;
mPath.moveTo(mLastX, mLastY);
break;
case MotionEvent.ACTION_MOVE:
int dx = Math.abs(x-mLastX);
int dy = Math.abs(y-mLastY);
if(dx > 3||dy >3){//移动小于3像素的时候不做改变
mPath.lineTo(x, y);
}
mLastX = x;
mLastY = y;
break;
case MotionEvent.ACTION_UP:
new Thread(mRunnable).start();
break;
default:
break;
}
invalidate();
return true;
}
private Runnable mRunnable = new Runnable() {
@Override
public void run() {
int w = getWidth();
int h = getHeight();
float wipeArea = 0;
float totalArea = w*h;
Bitmap bitmap = mBitmap;
int[] mPixels = new int[w*h];
//获得Bitmap上所有的像素信息
mBitmap.getPixels(mPixels,0,w,0,0,w,h);
for(int i = 0;i<w;i++){
for(int j = 0;j <h;j++){
int index = i+j*w;
if(mPixels[index]==0){
wipeArea++;
}
}
}
if(wipeArea>0&&totalArea>0){
int percent = (int)(wipeArea*100/totalArea);
Log.e("zx", percent+"");
if(percent>60){
mComplete = true;
postInvalidate();
}
}
}
};
@Override//图形绘制
protected void onDraw(Canvas canvas) {
canvas.drawText(mText, getWidth()/2-mTextBound.width()/2, getHeight()/2+mTextBound.height()/2, mBackPaint);
if(mComplete){
if(mListener!=null){
mListener.complete();
}
}
if(!mComplete){
drawPath();
canvas.drawBitmap(mBitmap, 0,0, null);
}
}
private void drawPath() {
mOutterPain.setXfermode(new PorterDuffXfermode(Mode.DST_OUT));
mCanvas.drawPath(mPath, mOutterPain);
}
}
分享一下学习网站,慕课网