日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

自定义斗鱼礼物动画

發(fā)布時間:2023/12/8 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 自定义斗鱼礼物动画 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

項目需求,仿著斗魚做了個自定義禮物的view,繪制圖片實現(xiàn),

效果大概就是這樣:

直接上代碼

<declare-styleable name="GiftView"><attr name="giftDrawable" format="reference"/><!--禮物圖片資源id--><attr name="xDrawable" format="reference"/><!--乘號圖片資源id--><attr name="numbers" format="integer"/><!--禮物數(shù)量--></declare-styleable>

自定義view

/*** Created by yinw on 2016-12-08.* 自定義禮物view*/public class GiftView extends View {private int mGiftId, mXId, numbers, maxNumber, measureHeight, measureWidth;private Paint paint;private Bitmap mGift, mX;private boolean isFirstLayout = true,withAni = false;private int levelSize = 0;//繪制圖片的大小等級private final int CHANGESIZE=10;//變更的大小public GiftView(Context context) {this(context, null);}public GiftView(Context context, AttributeSet attrs) {this(context, attrs, 0);}public GiftView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.GiftView, defStyleAttr, 0);mGiftId = typedArray.getResourceId(R.styleable.GiftView_giftDrawable, -1);mXId = typedArray.getResourceId(R.styleable.GiftView_xDrawable, -1);numbers = typedArray.getInt(R.styleable.GiftView_numbers, -1);typedArray.recycle();initGiftAndX();maxNumber = numbers;paint = new Paint();}private void initGiftAndX() {if (mXId != -1) {mX = ((BitmapDrawable) getResources().getDrawable(mXId)).getBitmap();}if (mGiftId != -1) {mGift = ((BitmapDrawable) getResources().getDrawable(mGiftId)).getBitmap();}}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {int heightMode = MeasureSpec.getMode(heightMeasureSpec);int widthMode = MeasureSpec.getMode(widthMeasureSpec);measureHeight = MeasureSpec.getSize(heightMeasureSpec);measureWidth = MeasureSpec.getSize(widthMeasureSpec);if (widthMode != MeasureSpec.EXACTLY) {if (mGift != null) {measureWidth = mGift.getWidth();}if (mX != null) {measureWidth += mX.getWidth();}if (maxNumber != -1) {measureWidth += getNumbersWidth(maxNumber);//按照最大數(shù)字}measureWidth += (getPaddingLeft() + getPaddingRight());}if (heightMode != MeasureSpec.EXACTLY) {if (mGift != null) {measureHeight = mGift.getHeight();}measureHeight += (getPaddingTop() + getPaddingBottom());}setMeasuredDimension(measureWidth, measureHeight);}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);if (mGift != null && mX != null && numbers != -1) {drawNumbers(canvas, numbers, drawGiftAndX(canvas));//只調用一次,以防影響性能if (isFirstLayout) {requestLayout();//重新計算寬高重新布局isFirstLayout = false;}}}/**** @param mGiftId 禮物資源id* @param mXId 乘號資源id* @param numbers 數(shù)字大小* @param withAni 是否數(shù)字遞增造成動畫效果還是直接繪制數(shù)字*/public void setGiftSettings(int mGiftId, int mXId, final int numbers, boolean withAni) {this.mGiftId = mGiftId;this.mXId = mXId;this.numbers = numbers;this.maxNumber = numbers;this.withAni = withAni;initGiftAndX();if (withAni) {new Thread(new Runnable() {@Overridepublic void run() {GiftView.this.numbers = 0;//改變數(shù)字的大小,通過重繪制造動畫效果while (GiftView.this.numbers < numbers) {//數(shù)字變回本來大小if (levelSize == 0) {GiftView.this.numbers++;}postInvalidate();try {Thread.sleep(400);} catch (InterruptedException e) {e.printStackTrace();}}}}).start();return;}invalidate();}//繪制禮物和符號private int drawGiftAndX(Canvas canvas) {int gXWidth = 0;if (mGift != null) {canvas.drawBitmap(mGift, getPaddingLeft(), getPaddingTop(), paint);gXWidth = mGift.getWidth() + getPaddingLeft();}Bitmap NMx=mX;if (mX != null) {if (withAni) {if (levelSize == 0) {levelSize = 1;} else {levelSize = 0;}if(levelSize==1) {NMx = Bitmap.createScaledBitmap(NMx, NMx.getWidth() + CHANGESIZE, NMx.getHeight() + CHANGESIZE, false);//圖片增大}}canvas.drawBitmap(NMx, gXWidth, (mGift.getHeight() - NMx.getHeight()) / 2 + getPaddingTop(), paint);gXWidth += NMx.getWidth();}return gXWidth;}//繪制出數(shù)字圖片private void drawNumbers(Canvas canvas, int numbers, int beginX) {String number = String.valueOf(numbers);int width = beginX;Bitmap bitmap;for (int i = 1; i <= number.length(); i++) {bitmap = resolveNumber(Integer.parseInt(number.substring(i - 1, i)));if (withAni) {if (levelSize == 1) {bitmap = Bitmap.createScaledBitmap(bitmap, bitmap.getWidth() + CHANGESIZE, bitmap.getHeight() + CHANGESIZE, false);//圖片增大}}canvas.drawBitmap(bitmap, width, (mGift.getHeight() - bitmap.getHeight()) / 2 + getPaddingTop(), paint);width += bitmap.getWidth();}}//解析出數(shù)字對應的資源圖片id,這里并沒有對圖片進行防溢出處理,因為圖片本身都很小,自己注意就行,以上也是private Bitmap resolveNumber(int number) {Bitmap numBitmap = null;switch (number) {case 0:numBitmap = ((BitmapDrawable) getResources().getDrawable(R.drawable.num0)).getBitmap();break;case 1:numBitmap = ((BitmapDrawable) getResources().getDrawable(R.drawable.num1)).getBitmap();break;case 2:numBitmap = ((BitmapDrawable) getResources().getDrawable(R.drawable.num2)).getBitmap();break;case 3:numBitmap = ((BitmapDrawable) getResources().getDrawable(R.drawable.num3)).getBitmap();break;case 4:numBitmap = ((BitmapDrawable) getResources().getDrawable(R.drawable.num4)).getBitmap();break;case 5:numBitmap = ((BitmapDrawable) getResources().getDrawable(R.drawable.num5)).getBitmap();break;case 6:numBitmap = ((BitmapDrawable) getResources().getDrawable(R.drawable.num6)).getBitmap();break;case 7:numBitmap = ((BitmapDrawable) getResources().getDrawable(R.drawable.num7)).getBitmap();break;case 8:numBitmap = ((BitmapDrawable) getResources().getDrawable(R.drawable.num8)).getBitmap();break;case 9:numBitmap = ((BitmapDrawable) getResources().getDrawable(R.drawable.num9)).getBitmap();break;}return numBitmap;}//計算數(shù)字長度private int getNumbersWidth(int numbers) {int width = 0;String number = String.valueOf(numbers);for (int i = 1; i <= number.length(); i++) {width += (resolveNumber(Integer.parseInt(number.substring(i - 1, i))).getWidth() + CHANGESIZE);}return width;}}

這里主要是對bitmap的一個小應用,對應的資源大家自己找美工吧~

程序員內功修煉手冊 不定期分享程序員基礎知識,大前端知識!想跟博主一塊成長的快快關注吧!

總結

以上是生活随笔為你收集整理的自定义斗鱼礼物动画的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。