给你Android实现的九宫格抽奖程序,2021百度Android岗面试真题收录解析

2022/1/29 22:05:07

本文主要是介绍给你Android实现的九宫格抽奖程序,2021百度Android岗面试真题收录解析,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

super(context, attrs, defStyleAttr);

init();

}

/**

  • 初始化数据

*/

private void init() {

mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

mPaint.setStyle(Paint.Style.FILL);

mPaint.setStrokeWidth(mStrokWidth);

mRects = new ArrayList<>();

}

@Override

protected void onSizeChanged(int w, int h, int oldw, int oldh) {

super.onSizeChanged(w, h, oldw, oldh);

mRectSize = Math.min(w, h)/3;//获取矩形的宽和高

mRects.clear();//当控件大小改变的时候清空数据

initRect();//重新加载矩形数据

}

/**

  • 加载矩形数据

*/

private void initRect() {

//加载前三个矩形

for(int x = 0;x<3;x++){

float left = x * mRectSize;

float top = 0;

float right = (x + 1) * mRectSize;

float bottom = mRectSize;

RectF rectF = new RectF(left,top,right,bottom);

mRects.add(rectF);

}

//加载第四个

mRects.add(new RectF(getWidth()-mRectSize,mRectSize,getWidth(),mRectSize * 2));

//加载第五~七个

for(int y= 3;y>0;y–){

float left = getWidth() - (4-y) * mRectSize;

float top = mRectSize * 2;

float right = (y - 3) * mRectSize+getWidth();

float bottom = mRectSize * 3;

RectF rectF = new RectF(left,top,right,bottom);

mRects.add(rectF);

}

//加载第八个

mRects.add(new RectF(0,mRectSize,mRectSize,mRectSize * 2));

//加载第九个

mRects.add(new RectF(mRectSize,mRectSize,mRectSiz
e*2,mRectSize * 2));

}

@Override

protected void onDraw(Canvas canvas) {

super.onDraw(canvas);

drawRects(canvas);

}

/**

  • 画矩形

  • @param canvas

*/

private void drawRects(Canvas canvas) {

for (int x = 0;x<mRects.size();x++){

RectF rectF = mRects.get(x);

if(x == 8){//中心的矩形背景为白色

mPaint.setColor(Color.WHITE);

canvas.drawRect(rectF, mPaint);

}else {

mPaint.setColor(mItemColor[x%2]);

canvas.drawRect(rectF, mPaint);

}

}

}

}

效果展示

2.添加奖品图片

我们在矩形的中心点画出图片,图片的宽高都为矩形宽高的1/2,其中矩形的中心点通过rectF.centerX()rectF.centerY()获取。

代码展示(为了方便只展示部分代码)

public class NineLuckPan extends View {

private int [] mImgs = {R.drawable.ic_df,R.drawable.ic_jt,R.drawable.ic_mf,R.drawable.ic_scjx,R.drawable.ic_scng,R.drawable.ic_thl,R.drawable.ic_x,R.drawable.ic_xc,R.drawable.ic_j};

@Override

protected void onDraw(Canvas canvas) {

super.onDraw(canvas);

drawRects(canvas);//画矩形

drawImages(canvas);//画图片

}

/**

  • 画图片

  • @param canvas

*/

private void drawImages(Canvas canvas) {

for (int x = 0;x<mRects.size();x++){

RectF rectF = mRects.get(x);

float left = rectF.centerX() - mRectSize / 4;

float top = rectF.centerY() - mRectSize / 4;

canvas.drawBitmap(Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(),mImgs[x]),mRectSize/2,mRectSize/2,false),left,top,null);

}

}

}

效果展示

3.实现抽奖动画

这里我们使用ValueAnimator使数字从0递增到我们的中奖位置比如3,然后改变递增所在的位置的矩形的背景以实现抽奖的效果,另外为了让用户感到逼真我们需要让抽奖色块多转几圈,同时我们需要将下一次的开始位置设为上一次的结束位置。

代码展示(为了方便只展示部分代码)

NineLuckPan:

public class NineLuckPan extends View {

private int mRepeatCount = 3;//转的圈数

private int mLuckNum = 3;//最终中奖位置

private int mPosition = -1;//抽奖块的位置

private int mStartLuckPosition = 0;//开始抽奖的位置

/**

  • 画矩形

  • @param canvas

*/

private void drawRects(Canvas canvas) {

for (int x = 0;x<mRects.size();x++){

RectF rectF = mRects.get(x);

if(x == 8){

mPaint.setColor(Color.WHITE);

canvas.drawRect(rectF, mPaint);

}else {

mPaint.setColor(mItemColor[x%2]);

if(mPosition == x){

mPaint.setColor(Color.BLUE);

}

canvas.drawRect(rectF, mPaint);

}

}

}

public void setPosition(int position){

mPosition = position;

invalidate();

}

/**

  • 开始动画

*/

public void startAnim(){

ValueAnimator valueAnimator = ValueAnimator.ofInt(mStartLuckPosition, mRepeatCount * 8 + mLuckNum).setDuration(5000);

valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

@Override

public void onAnimationUpdate(ValueAnimator animation) {

int position = (int) animation.getAnimatedValue();
nim(){

ValueAnimator valueAnimator = ValueAnimator.ofInt(mStartLuckPosition, mRepeatCount * 8 + mLuckNum).setDuration(5000);

valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

@Override

public void onAnimationUpdate(ValueAnimator animation) {

int position = (int) animation.getAnimatedValue();



这篇关于给你Android实现的九宫格抽奖程序,2021百度Android岗面试真题收录解析的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程