java类:RandomView
package com.example.drawablerander.view;
import java.util.ArrayList;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
/**
* 模拟随机中奖的九宫格:
*
*
*
*/
@SuppressLint("ClickableViewAccessibility")
public class RandomView extends View {
private String TAG = RandomView.class.getSimpleName();
private Bitmap mBitmapSrc;
private Bitmap blackBitmap;
private Paint paint = new Paint();
private Bitmap[] bitmaps = new Bitmap[12];
private boolean startOpenThreadFlag = false;
private float fouceX = 0;
private float fouceY = 0;
public RandomView(Context context) {
super(context);
intView();
}
public RandomView(Context context, AttributeSet attrs) {
super(context, attrs);
intView();
}
public RandomView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
intView();
}
private int bitmapSrcW;
private int bitmapSrcH;
private int xBitmapW;
private int xBitmapH;
/**
* 初始化
*/
private void intView() {
BitmapDrawable bitmap = (BitmapDrawable) getBackground();
mBitmapSrc = bitmap.getBitmap();
bitmapSrcW = mBitmapSrc.getWidth();
bitmapSrcH = mBitmapSrc.getHeight();
xBitmapW = bitmapSrcW / 4;
xBitmapH = bitmapSrcH / 4;
blackBitmap = Bitmap.createBitmap(xBitmapW, xBitmapH, Config.RGB_565);
}
@SuppressLint("NewApi")
@Override
public boolean onTouchEvent(MotionEvent event) {
float eventX = 0;
float eventY = 0;
float centerMin_x = getOnTouchCenterFectMinX();// 获取点击事件的中间区域的最小坐标值;
float centerMin_y = getOnTouchCenterFectMinY();// 获取点击事件的中间区域的最小坐标值;
float centerMax_x = getOnTochCenterMaxX();
float centerMax_y = getOnTochCenterMaxY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
eventX = event.getX();
eventY = event.getY();
if (centerMin_x < eventX && eventX < centerMax_x
&& centerMin_y < eventY && eventY < centerMax_y) {
startOpenThreadFlag = true;
Log.d(TAG, "startOpenThreadFlag="+startOpenThreadFlag);
if (myThread == null) {
myThread = new Mythread();
myThread.start();
}
}
Log.d(TAG, "eventX---=" + eventX);
Log.d(TAG, "eventY---=" + eventY);
break;
case MotionEvent.ACTION_UP:
//
startOpenThreadFlag = false;
break;
case MotionEvent.ACTION_MOVE:
break;
default:
break;
}
return true;
}
/**
* 获取点击区域的最大值;
*
* @return
*/
private float getOnTochCenterMaxY() {
return dstHeight * 3;
}
/**
* 获取点击区域的最大值;
*
* @return
*/
private float getOnTochCenterMaxX() {
return dstWidth * 3;
}
/**
* 获取点击事件的中间区域的起始x坐标
*
* @return
*/
private float getOnTouchCenterFectMinX() {
return dstWidth;
}
/**
* 获取中间区域的其实坐标;
*
* @return
*/
private float getOnTouchCenterFectMinY() {
return dstHeight;
}
private int mViewH;
private int mViewW;
private Bitmap endBitmap;
private Bitmap scaledBitmap;
private int dstWidth;// 屏幕单个小格子的宽度;
private int dstHeight;// 屏幕单个小格子的高度;
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mViewW = getWidth();
mViewH = getHeight();
dstWidth = mViewW / 4;
dstHeight = mViewH / 4;
endBitmap = creatScaleBitmap(blackBitmap);
scaledBitmap = creatScaleBitmapsrc();
spiltScaledBitmap();
drawSub(canvas);
}
private ArrayList
framViews;
/**
* 分割缩放后的源资源图片
*/
private void spiltScaledBitmap() {
framViews = new ArrayList();
bitmapSrcW = scaledBitmap.getWidth();
bitmapSrcH = scaledBitmap.getHeight();
Log.d(TAG, "bitmapSrcW" + bitmapSrcW);
Log.d(TAG, "bitmapSrcH" + bitmapSrcH);
xBitmapW = bitmapSrcW / 4;
xBitmapH = bitmapSrcH / 4;
addData();
}
private void addData() {
addFirst();
addSecondRight();
addThirdRight();
addBottom();
addThirdLeft();
addSecondLeft();
}
private void addFirst() {
for (int i = 0; i < 4; i++) {// 第一行,
if (i < 4) {
bitmaps[i] = Bitmap.createBitmap(scaledBitmap, xBitmapW * i, 0,
xBitmapW, xBitmapH);
SmallFramView framView = new SmallFramView();
framView.setBitmap(bitmaps[i]);
framView.setId(i);
framView.setPostx(xBitmapW * i);
framView.setPosty(0);
framViews.add(framView);
}
}
}
/**
*
*/
private void addSecondLeft() {
bitmaps[11] = Bitmap.createBitmap(scaledBitmap, 0, xBitmapH * 1,
xBitmapW, xBitmapH);
SmallFramView framView = new SmallFramView();
framView.setBitmap(bitmaps[11]);
framView.setId(11);
framView.setPostx(0);
framView.setPosty(xBitmapH * 1);
framViews.add(framView);
}
private void addThirdLeft() {
bitmaps[10] = Bitmap.createBitmap(scaledBitmap, 0, xBitmapH * 2,
xBitmapW, xBitmapH);
SmallFramView framView = new SmallFramView();
framView.setBitmap(bitmaps[10]);
framView.setId(10);
framView.setPostx(0);
framView.setPosty(xBitmapH * 2);
framViews.add(framView);
}
private void addBottom() {
for (int i = 0; i < 4; i++) {
bitmaps[6 + i] = Bitmap.createBitmap(scaledBitmap, xBitmapW
* (3 - i), xBitmapH * 3, xBitmapW, xBitmapH);
SmallFramView framView = new SmallFramView();
framView.setBitmap(bitmaps[6 + i]);
framView.setId(6 + i);
framView.setPostx(xBitmapW * (3 - i));
framView.setPosty(xBitmapH * 3);
framViews.add(framView);
}
}
/**
* 添加第三行右边的;
*/
private void addThirdRight() {
bitmaps[5] = Bitmap.createBitmap(scaledBitmap, xBitmapW * 3,
xBitmapH * 2, xBitmapW, xBitmapH);
SmallFramView framView = new SmallFramView();
framView.setBitmap(bitmaps[5]);
framView.setId(7);
framView.setPostx(xBitmapW * 3);
framView.setPosty(xBitmapH * 2);
framViews.add(framView);
}
/**
* 添加第二行右边的格子;
*/
private void addSecondRight() {
bitmaps[5] = Bitmap.createBitmap(scaledBitmap, xBitmapW * 3,
xBitmapH * 1, xBitmapW, xBitmapH);
SmallFramView framView = new SmallFramView();
framView.setBitmap(bitmaps[5]);
framView.setId(5);
framView.setPostx(xBitmapW * 3);
framView.setPosty(xBitmapH * 1);
framViews.add(framView);
}
/**
* 把原始图片缩放到等相对应的单个小格子屏幕尺寸大小;
*
* @param bitmap
* @return
*/
private Bitmap creatScaleBitmap(Bitmap bitmap) {
return Bitmap.createScaledBitmap(bitmap, dstWidth, dstHeight, true);
}
/**
* 原图缩放到显示控件区域大小;
*
* @return
*/
private Bitmap creatScaleBitmapsrc() {
return Bitmap.createScaledBitmap(mBitmapSrc, mViewW, mViewW, true);
}
private Thread myThread;
private void drawSub(Canvas canvas) {
SmallFramView framView = framViews.get(count);
canvas.drawBitmap(endBitmap, framView.getPostx(), framView.getPosty(),
paint);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(setmeasuredWidth(widthMeasureSpec),
setMeasuredHeight(heightMeasureSpec));
}
private int count = 0;
public class Mythread extends Thread {
public Mythread() {
}
@Override
public void run() {
while (startOpenThreadFlag) {
count++;
if (count == bitmaps.length) {
count = 0;
}
postInvalidate();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
/**
* 对宽度重新设置默认值;
*
* @param widthMeasureSpec
* @return
*/
private int setmeasuredWidth(int widthMeasureSpec) {
int result = 0;
int specMode = MeasureSpec.getMode(widthMeasureSpec);
int specSize = MeasureSpec.getSize(widthMeasureSpec);
if (specMode == MeasureSpec.EXACTLY) {// fill
result = specSize;
} else {
result = 200;
if (specMode == MeasureSpec.AT_MOST) {// wrap
result = Math.min(result, specSize);
}
}
return result;
}
/**
* 对高度重新测量
*
* @param heightMeasureSpec
* @return
*/
private int setMeasuredHeight(int heightMeasureSpec) {
int result = 0;
int specMode = MeasureSpec.getMode(heightMeasureSpec);
int specSize = MeasureSpec.getSize(heightMeasureSpec);
if (specMode == MeasureSpec.EXACTLY) {
result = specSize;
} else {
result = 200;
if (specMode == MeasureSpec.AT_MOST) {
result = Math.min(result, specSize);
}
}
return result;
}
}
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
java类:
package com.example.drawablerander.view;
import android.graphics.Bitmap;
public class SmallFramView {
private int id;
private Bitmap bitmap;
private int postx;// x坐标
private int posty;// y坐标
public SmallFramView() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Bitmap getBitmap() {
return bitmap;
}
public void setBitmap(Bitmap bitmap) {
this.bitmap = bitmap;
}
public int getPostx() {
return postx;
}
public void setPostx(int postx) {
this.postx = postx;
}
public int getPosty() {
return posty;
}
public void setPosty(int posty) {
this.posty = posty;
}
}