Android之十三刮刮卡中奖功能



package com.zhy.view;import android.content.Context;import android.graphics.Bitmap;import android.graphics.Bitmap.Config;import android.graphics.Paint.Style;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.graphics.Rect;import android.graphics.RectF;import android.support.v4.widget.SlidingPaneLayout.PanelSlideListener;import android.util.AttributeSet;import android.util.Log;import android.view.MotionEvent;import android.view.View;import com.example.zhy_path.R;public class GuaGuaKa extends View{/** * 绘制线条的Paint,即用户手指绘制Path */private Paint mOutterPaint = new Paint();/** * 记录用户绘制的Path */private Path mPath = new Path();/** * 内存中创建的Canvas */private Canvas mCanvas;/** * mCanvas绘制内容在其上 */private Bitmap mBitmap;/** * ------------------------以下是奖区的一些变量 */// private Bitmap mBackBitmap;private boolean isComplete;private Paint mBackPint = new Paint();private Rect mTextBound = new Rect();private String mText = "¥500";private int mLastX;private int mLastY;public GuaGuaKa(Context context){this(context, null);}public GuaGuaKa(Context context, AttributeSet attrs){this(context, attrs, 0);}public GuaGuaKa(Context context, AttributeSet attrs, int defStyle){super(context, attrs, defStyle);init();}private void init(){mPath = new Path();// mBackBitmap = BitmapFactory.decodeResource(getResources(),// R.drawable.t2);setUpOutPaint();setUpBackPaint();}/** * 初始化canvas的绘制用的画笔 */private void setUpBackPaint(){mBackPint.setStyle(Style.FILL);mBackPint.setTextScaleX(2f);mBackPint.setColor(Color.RED);mBackPint.setTextSize(32);mBackPint.getTextBounds(mText, 0, mText.length(), mTextBound);}@Overrideprotected void onDraw(Canvas canvas){// canvas.drawBitmap(mBackBitmap, 0, 0, null);// 绘制奖项canvas.drawText(mText, getWidth() / 2 - mTextBound.width() / 2,getHeight() / 2 + mTextBound.height() / 2, mBackPint);if (!isComplete){drawPath();canvas.drawBitmap(mBitmap, 0, 0, null);}}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){super.onMeasure(widthMeasureSpec, heightMeasureSpec);int width = getMeasuredWidth();int height = getMeasuredHeight();// 初始化bitmapmBitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);mCanvas = new Canvas(mBitmap);// 绘制遮盖层// mCanvas.drawColor(Color.parseColor("#c0c0c0"));mOutterPaint.setStyle(Paint.Style.FILL);mCanvas.drawRoundRect(new RectF(0, 0, width, height), 30, 30,mOutterPaint);mCanvas.drawBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.s_title), null, new RectF(0, 0, width, height), null);}/** * 设置画笔的一些参数 */private void setUpOutPaint(){// 设置画笔// mOutterPaint.setAlpha(0);mOutterPaint.setColor(Color.parseColor("#c0c0c0"));mOutterPaint.setAntiAlias(true);mOutterPaint.setDither(true);mOutterPaint.setStyle(Paint.Style.STROKE);mOutterPaint.setStrokeJoin(Paint.Join.ROUND); // 圆角mOutterPaint.setStrokeCap(Paint.Cap.ROUND); // 圆角// 设置画笔宽度mOutterPaint.setStrokeWidth(20);}/** * 绘制线条 */private void drawPath(){mOutterPaint.setStyle(Paint.Style.STROKE);mOutterPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));mCanvas.drawPath(mPath, mOutterPaint);}@Overridepublic 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)mPath.lineTo(x, y);mLastX = x;mLastY = y;break;case MotionEvent.ACTION_UP:new Thread(mRunnable).start();break;}invalidate();return true;}/** * 统计擦除区域任务 */private Runnable mRunnable = new Runnable(){private int[] mPixels;public void run(){int w = getWidth();int h = getHeight();float wipeArea = 0;float totalArea = w * h;Bitmap bitmap = mBitmap;mPixels = new int[w * h];/** * 拿到所有的像素信息 */bitmap.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("TAG", percent + "");if (percent > 70){Log.e("TAG", "清除区域达到70%,下面自动清除");isComplete = true;postInvalidate();}}}};}
MainActivity.java
package com.example.zhy_path;import android.os.Bundle;import android.app.Activity;import android.view.Menu;public class MainActivity extends Activity{@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}<span style="font-size:18px;color:#ff0000;">}</span>
activity_main.xml
<RelativeLayout 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" >    <com.zhy.view.GuaGuaKa        android:layout_centerInParent="true"        android:layout_width="300dp"        android:layout_height="100dp" /></RelativeLayout>



更多相关文章

  1. android的ListView图文混搭
  2. Android窗口机制(四)ViewRootImpl与View和WindowManager
  3. Android自定义radiobutton(文字靠左,选框靠右)
  4. 可点击价格走势图-贝塞尔曲线
  5. Android图表控件MPAndroidChart——LineChart实现 XY轴、原点线
  6. Android(安卓)VectorDrawable与SVG
  7. android 自定义控件全系列导航
  8. Android(安卓)Shape 详解
  9. Android(安卓)OpenGL ES 开发教程 从入门到精通

随机推荐

  1. android 中 LocalSocket的基本使用方法
  2. Android 2.3 Rotation and Orientation
  3. android百度地图半径画圆
  4. Android java List 转Json格式
  5. 升级android studio后,打包的apk无法访问
  6. ANDROID与.Net之间JSON实践
  7. Android学习笔记(5)——Android——Hello
  8. Android(安卓)IPC机制(三):浅谈Binder的
  9. Android黑名单来电自动静音源码
  10. 开源android项目分享