Android实现点赞动画(27)

本文实例为大家分享了Android使用入门第二十七篇点赞动画的具体代码,供大家参考,具体内容如下

MainActivity.java代码:

package siso.likeanimation;

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;

public class MainActivity extends AppCompatActivity {
 private DivergeViewSecond mDivergeView;
 private Button mBtnStart;
 private ImageView mImageView;
 private ArrayList<Bitmap> mList;
 private int mIndex = 0;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 mBtnStart = (Button)findViewById(R.id.btnStart);
 mImageView = (ImageView)findViewById(R.id.iv_start);
 mList = new ArrayList<>();
 mList.add(((BitmapDrawable) ResourcesCompat.getDrawable(getResources(), R.drawable.ic_praise_sm1, null)).getBitmap());
 mList.add(((BitmapDrawable) ResourcesCompat.getDrawable(getResources(),R.drawable.ic_praise_sm2,null)).getBitmap());
 mList.add(((BitmapDrawable) ResourcesCompat.getDrawable(getResources(),R.drawable.ic_praise_sm3,null)).getBitmap());
 mList.add(((BitmapDrawable) ResourcesCompat.getDrawable(getResources(),R.drawable.ic_praise_sm4,null)).getBitmap());
 mList.add(((BitmapDrawable) ResourcesCompat.getDrawable(getResources(),R.drawable.ic_praise_sm5,null)).getBitmap());
 mList.add(((BitmapDrawable) ResourcesCompat.getDrawable(getResources(), R.drawable.ic_praise_sm6, null)).getBitmap());
 mBtnStart.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
  if(mIndex == 5){
   mIndex = 0 ;
  }
  mDivergeView.startDiverges(mIndex);
  mIndex ++;
//  if (mDivergeView.isRunning()) {
//   mDivergeView.stop();
//  } else {
//
//  }
  }
 });
 mDivergeView = (DivergeViewSecond) findViewById(R.id.divergeView);
 mDivergeView.post(new Runnable() {
  @Override
  public void run() {
  mDivergeView.setEndPoint(new PointF(mDivergeView.getMeasuredWidth()/2,0));
  mDivergeView.setDivergeViewProvider(new Provider());
  }
 });
// mDivergeView.start();
 }

 @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);
 }
 }
}

DivergeViewSecond.java代码:

package siso.likeanimation;

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;
import java.util.concurrent.CopyOnWriteArrayList;

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<DivergeInfo> mDivergeInfos;

 protected List<Object> mQueen;

 protected PointF mPtStart;
 protected PointF mPtEnd;

 protected ArrayList<DivergeInfo> 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 divergeInfo = mDivergeInfos.get(i);

  float timeLeft = 1.0F - divergeInfo.mDuration;

  divergeInfo.mDuration += mDuration;

  float x, y;

  //二次贝塞尔
  float time1 = timeLeft * timeLeft;
  float time2 = 2 * timeLeft * divergeInfo.mDuration;
  float time3 = divergeInfo.mDuration * divergeInfo.mDuration;
  x = time1 * (mPtStart.x)
   + time2 * (divergeInfo.mBreakPoint.x)
   + time3 * (divergeInfo.mEndPoint.x);

  divergeInfo.mX = x;

  y = time1 * (mPtStart.y)
   + time2 * (divergeInfo.mBreakPoint.y)
   + time3 * (divergeInfo.mEndPoint.y);

  divergeInfo.mY = y;

  if (divergeInfo.mY <= divergeInfo.mEndPoint.y) {
  mDivergeInfos.remove(i);
  mDeadPool.add(divergeInfo);
  i--;
  continue;
  }
 }
 }

 private void dealQueen(){
 long now = System.currentTimeMillis();
 if(mQueen.size() > 0 && now - mLastAddTime > mQueenDuration){
  mLastAddTime = System.currentTimeMillis();
  DivergeInfo divergeInfo = null;
  if(mDeadPool.size() > 0){
  //死池里面有空闲的divergeNode
  divergeInfo = mDeadPool.get(0);
  mDeadPool.remove(0);
  }
  if(divergeInfo == null){
  divergeInfo = createDivergeNode(mQueen.get(0));
  }
  divergeInfo.reset();
  divergeInfo.mType = mQueen.get(0);
  mDivergeInfos.add(divergeInfo);
  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 divergeViewProvider){
 mDivergeViewProvider = divergeViewProvider;
 }

 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 divergeInfo : mDivergeInfos){
  mPaint.setAlpha((int)(255 * divergeInfo.mY / mPtStart.y));
  canvas.drawBitmap(mDivergeViewProvider.getBitmap(divergeInfo.mType),
   divergeInfo.mX,
   divergeInfo.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;
 }
 }
}

activity_main.xml内容:

<?xml version="1.0" encoding="utf-8"?>
<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"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="siso.likeanimation.MainActivity">

 <Button
 android:id="@+id/btnStart"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:text="开始"/>

 <siso.likeanimation.DivergeViewSecond
 android:id="@+id/divergeView"
 android:layout_alignParentBottom="true"
 android:layout_alignParentRight="true"
 android:layout_width="130dp"
 android:layout_height="300dp"/>
 <ImageView
 android:id="@+id/iv_start"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_marginRight="10dp"
 android:layout_alignParentBottom="true"
 android:layout_alignParentRight="true"
 android:background="@drawable/ic_praise_sm6"/>
</RelativeLayout>

运行结果如图:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • Android的消息机制

    一.简介 Android的消息机制主要是指Handler的运行机制,那么什么是Handler的运行机制那?通俗的来讲就是,使用Handler将子线程的Message放入主线程的Messagequeue中,在主线程使用. 二.学习内容 学习Android的消息机制,我们需要先了解如下内容. 消息的表示:Message 消息队列:MessageQueue 消息循环,用于循环取出消息进行处理:Looper 消息处理,消息循环从消息队列中取出消息后要对消息进行处理:Handler 平常我们接触的大多是H

  • Android5.x中的阴影效果elevation和translationZ的实现方法

    android5.x中 view多了一个 属性 z,垂直高度上的变化. Z属性  由两部分组成,elevation 和 translationZ <ImageView android:id="@+id/me_daijjBack" android:layout_width="27dp" android:layout_height="27dp" android:elevation="3dp" android:translat

  • 详解Android Material设计中阴影效果的实现方法

    View可以投下的阴影,一个View的elevation值决定了它的阴影的大小和绘制的顺序.可以设置一个视图的elevation,在布局中使用属性:android:elevation <TextView android:id="@+id/my_textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=&quo

  • Android 自定义阴影效果详解及实例

    Android 自定义阴影效果详解及实例 Android5.X中,Google为其增加了两个属性 android:elevation=" " 与 android:translationZ=" ",对应垂直方向上的高度变化.系统会自动增加阴影效果. 在TabLayout中增加android:elevation=" 8dp" ,效果如下: 箭头指向的就是系统为我们默认提供,结果差强人意.那我们是不是可以自定义阴影,不使用系统提供的. 自定义阴影效果

  • Android编程之通知栏的用法小结

    本文实例总结了Android编程中通知栏的用法.分享给大家供大家参考,具体如下: 很久没有使用Android的通知功能了,今天把两年前的代码搬出来一看,发现很多方法都废弃了,代码中各种删除线看的十分不爽.于是乎,打开Google,查看官方文档,学习最新的发送通知栏消息的方法. 本文中的代码均参照谷歌官方文档编写: http://developer.android.com/guide/topics/ui/notifiers/notifications.html 1.首先,获取系统的通知服务: 复制

  • Android编程使用自定义shape实现shadow阴影效果的方法

    本文实例讲述了Android编程使用自定义shape实现shadow阴影效果的方法.分享给大家供大家参考,具体如下: 直接上xml文件, 并且附上相应的解析: <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_p

  • Android 解决监听home键的几种方法

    Android 解决监听home键的几种方法 前言: 以下两种方法可以完美解决监听back键,home键,多任务键(最近任务键). 一.使用注册广播监听home键.多任务键 演示图 创建一个广播代码如下: class InnerRecevier extends BroadcastReceiver { final String SYSTEM_DIALOG_REASON_KEY = "reason"; final String SYSTEM_DIALOG_REASON_RECENT_APP

  • Android编程实现canvas绘制饼状统计图功能示例【自动适应条目数量与大小】

    本文实例讲述了Android编程实现canvas绘制饼状统计图功能.分享给大家供大家参考,具体如下: 本例的目的是实现一个简单的饼状统计图,效果如下:    特点: 1.使用非常方便,可放在xml布局文件中,然后在代码中设置内容,即: PieChartView pieChartView = (PieChartView) findViewById(R.id.pie_chart); PieChartView.PieItemBean[] items = new PieChartView.PieItem

  • Android实现图片阴影效果的方法

    本文实例介绍了Android实现图片阴影效果,设置画布颜色,图像倾斜效果,图片阴影效果的方法,采用canvas.save(Canvas.MATRIX_SAVE_FLAG);来实现.由于图片的实际尺寸比显示出来的图像要大一些,因此需要适当更改下大小,以达到较好的效果,在原有矩形基础上,画成圆角矩形,同时带有阴影层.读者可以根据自身需要对该程序代码进行个性化的修改以便更符合自身项目需求. 具体实现代码如下: package canvas.test; import android.app.Activi

  • Android 操作excel功能实例代码

    学习app对excel的读写控制 1.界面设计 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id

随机推荐