android實現點贊飄心

android實現點贊飄心。先看看效果html

\

-----------------------------------佈局裏的代碼--------------------------------------------java

activity_mainandroid

?canvas

1app

2dom

3ide

4oop

5佈局

6post

7

8

<relativelayout android:background="@drawable/b1" android:layout_height="match_parent" android:layout_width="match_parent" xmlns:android="https://schemas.android.com/apk/res/android">

 

 

    <bwie.com.shuxing_dianzan.pergeviewsecond android:id="@+id/pergeView" android:layout_alignparentbottom="true" android:layout_centerhorizontal="true" android:layout_height="match_parent" android:layout_width="match_parent">

    <imageview android:id="@+id/iv_start" android:layout_alignparentbottom="true" android:layout_alignparentright="true" android:layout_height="match_parent" android:layout_marginright="10dp" android:layout_width="match_parent">

 

 

</imageview></bwie.com.shuxing_dianzan.pergeviewsecond></relativelayout>

----------------------下面就是MainActivity代碼-----------------

這裏面的圖片是本身定義的,能夠去本身的相冊裏導入本身的表情和圖片

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

import android.graphics.Bitmap;

import android.graphics.PointF;

import android.graphics.drawable.BitmapDrawable;

import android.support.v4.content.res.ResourcesCompat;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.ImageView;

 

import java.util.ArrayList;

import java.util.List;

 

 

public class MainActivity extends AppCompatActivity {

    private DivergeViewSecond mDivergeView;

    private ImageView mImageView;

    private List<bitmap> mList=new ArrayList<bitmap>();

    private int mIndex = 0;

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        mImageView =findViewById(R.id.iv_start);

        mList = new ArrayList<>();

        mList.add(((BitmapDrawable) ResourcesCompat.getDrawable(getResources(), R.drawable.a2, null)).getBitmap());

        mList.add(((BitmapDrawable) ResourcesCompat.getDrawable(getResources(),R.drawable.a3,null)).getBitmap());

        mList.add(((BitmapDrawable) ResourcesCompat.getDrawable(getResources(),R.drawable.a4,null)).getBitmap());

        mList.add(((BitmapDrawable) ResourcesCompat.getDrawable(getResources(),R.drawable.a5,null)).getBitmap());

        mList.add(((BitmapDrawable) ResourcesCompat.getDrawable(getResources(),R.drawable.a6,null)).getBitmap());

        mList.add(((BitmapDrawable) ResourcesCompat.getDrawable(getResources(), R.drawable.a7, null)).getBitmap());

        mList.add(((BitmapDrawable) ResourcesCompat.getDrawable(getResources(), R.drawable.a8, null)).getBitmap());

        mList.add(((BitmapDrawable) ResourcesCompat.getDrawable(getResources(), R.drawable.a9, null)).getBitmap());

        //mList.add(((BitmapDrawable) ResourcesCompat.getDrawable(getResources(), R.drawable.a10, null)).getBitmap());

        mImageView.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                if(mIndex == 8){

                    mIndex = 0 ;

                }

                mDivergeView.startDiverges(mIndex);

                mIndex ++;

            }

        });

        mDivergeView =findViewById(R.id.pergeView);

        mDivergeView.post(new Runnable() {

            @Override

            public void run() {

                mDivergeView.setEndPoint(new PointF(mDivergeView.getMeasuredWidth()/2,0));

                mDivergeView.setDivergeViewProvider(new Provider());

            }

        });

    }

    @Override

    protected void onDestroy() {

        super.onDestroy();

        if(mList != null){

            mList.clear();

            mList = null;

        }

 

    }

 

    class Provider implements DivergeViewSecond.DivergeViewProvider{

 

        @Override

        public Bitmap getBitmap(Object obj) {

            return mList == null ? null : mList.get((int)obj);

        }

    }

 

}</bitmap></bitmap>

?

1

<strong>DivergeViewSecond類</strong>

?

1

import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.PointF; import android.util.AttributeSet; import android.util.Log; import android.view.View; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Random; /** * Created by BAIPEI on 2017/12/8. */ public class DivergeViewSecond extends View implements Runnable { public static final float mDuration = 0.010F; public static final int mDefaultHeight = 100; protected static final long mQueenDuration = 200; protected final Random mRandom = new Random(); protected ArrayList<pergeinfo> mDivergeInfos; protected List<object>mQueen; protected PointF mPtStart; protected PointF mPtEnd; protected ArrayList<pergeinfo> mDeadPool = new ArrayList<>(); private Paint mPaint; // private static final int mDefaultWidth = 100; // private static final int mAlphaOffset = 50; private DivergeViewProvider mDivergeViewProvider; private long mLastAddTime = 0; private Thread mThread; private boolean mRunning = true; private boolean mIsDrawing = false; public DivergeViewSecond(Context context) { this(context, null); } public DivergeViewSecond(Context context, AttributeSet attrs) { this(context, attrs, 0); } public DivergeViewSecond(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } /** * Loop */ @Override public void run() { while(mRunning){ if(mDivergeViewProvider == null){ continue; } if(mQueen == null){ continue; } if(mIsDrawing){ //若是正在繪製,不要處理數據 continue; } if(mDivergeInfos == null){ continue; } dealQueen(); if(mDivergeInfos.size() == 0){ continue; } dealDiverge(); mIsDrawing = true; postInvalidate(); } //中止 release(); } private void dealDiverge(){ for (int i = 0; i < mDivergeInfos.size(); i++) { DivergeInfo pergeInfo = mDivergeInfos.get(i); float timeLeft = 1.0F - pergeInfo.mDuration; pergeInfo.mDuration += mDuration; float x, y; //二次貝塞爾 float time1 = timeLeft * timeLeft; float time2 = 2 * timeLeft * pergeInfo.mDuration; float time3 = pergeInfo.mDuration * pergeInfo.mDuration; x = time1 * (mPtStart.x) + time2 * (pergeInfo.mBreakPoint.x) + time3 * (pergeInfo.mEndPoint.x); pergeInfo.mX = x; y = time1 * (mPtStart.y) + time2 * (pergeInfo.mBreakPoint.y) + time3 * (pergeInfo.mEndPoint.y); pergeInfo.mY = y; if (pergeInfo.mY <= pergeInfo.mEndPoint.y) { mDivergeInfos.remove(i); mDeadPool.add(pergeInfo); i--; continue; } } } private void dealQueen(){ long now = System.currentTimeMillis(); if(mQueen.size() > 0 && now - mLastAddTime > mQueenDuration){ mLastAddTime = System.currentTimeMillis(); DivergeInfo pergeInfo = null; if(mDeadPool.size() > 0){ //死池裏面有空閒的pergeNode pergeInfo = mDeadPool.get(0); mDeadPool.remove(0); } if(pergeInfo == null){ pergeInfo = createDivergeNode(mQueen.get(0)); } pergeInfo.reset(); pergeInfo.mType = mQueen.get(0); mDivergeInfos.add(pergeInfo); mQueen.remove(0); } } public interface DivergeViewProvider{ public Bitmap getBitmap(Object obj); } private void init(){ mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); //不須要支持wrap_content } @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { super.onLayout(changed, left, top, right, bottom); } public void setDivergeViewProvider(DivergeViewProvider pergeViewProvider){ mDivergeViewProvider = pergeViewProvider; } public PointF getStartPoint(){ return mPtStart; } public boolean isRunning(){ return mRunning; } public void startDiverges(Object obj){ if(mDivergeInfos == null){ mDivergeInfos = new ArrayList<>(30); } if(mQueen == null){ mQueen = Collections.synchronizedList(new ArrayList<>(30)); } mQueen.add(obj); // for(Object obj : objs) { // mQueen.add(obj); // } if(mThread == null) { mThread = new Thread(this); mThread.start(); } } public void stop(){ if(mDivergeInfos != null){ mDivergeInfos.clear(); } if(mQueen != null){ mQueen.clear(); } if(mDeadPool != null){ mDeadPool.clear(); } } public void release(){ stop(); mPtEnd = null; mPtStart = null; mDivergeInfos = null; mQueen = null; mDeadPool = null; } public void setStartPoint(PointF point){ mPtStart = point; } public void setEndPoint(PointF point){ mPtEnd = point; } @Override protected void onDetachedFromWindow() { super.onDetachedFromWindow(); mRunning = false; } @Override protected void onDraw(Canvas canvas) { if(mRunning && mDivergeViewProvider != null && mDivergeInfos != null){ for(DivergeInfo pergeInfo : mDivergeInfos){ mPaint.setAlpha((int)(255 * pergeInfo.mY / mPtStart.y)); canvas.drawBitmap(mDivergeViewProvider.getBitmap(pergeInfo.mType), pergeInfo.mX, pergeInfo.mY, mPaint); } } mIsDrawing = false; } private PointF getBreakPointF(int scale1, int scale2) { PointF pointF = new PointF(); pointF.x = mRandom.nextInt((getMeasuredWidth() - getPaddingRight() + getPaddingLeft()) / scale1) + getMeasuredWidth() / scale2; pointF.y = mRandom.nextInt((getMeasuredHeight() - getPaddingBottom() + getPaddingTop()) / scale1) + getMeasuredHeight() / scale2; return pointF; } protected DivergeInfo createDivergeNode(Object type){ PointF endPoint = mPtEnd; if(endPoint == null){ endPoint = new PointF(mRandom.nextInt(getMeasuredWidth()),0); } // int height = mDivergeViewProvider == null ? mDefaultHeight : mDivergeViewProvider.getBitmap(type).getHeight(); if(mPtStart == null) { mPtStart = new PointF(getMeasuredWidth() / 2, getMeasuredHeight() - mDefaultHeight);//默認起始高度 } return new DivergeInfo( mPtStart.x, mPtStart.y, getBreakPointF(2, 3), endPoint, type); } public class DivergeInfo { public float mDuration; public PointF mBreakPoint; public PointF mEndPoint; public float mX; public float mY; public Object mType; public float mStartX; public float mStartY; public DivergeInfo(float x, float y, PointF breakPoint, PointF endPoint, Object type){ mDuration = 0.0f; mEndPoint = endPoint; mX = x; mY = y; mStartX = x; mStartY = y; mBreakPoint = breakPoint; mType = type; } public void reset(){ mDuration = 0.0f; mX = mStartX; mY = mStartY; } } }</pergeinfo></object></pergeinfo>

?

1

簡簡單單的代碼實現了簡簡單單的點贊飄心

大笑大笑大笑

註釋有點少啊,若有bug,多多留言!

相關文章
相關標籤/搜索