欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android实现点赞动画(27)

程序员文章站 2023-12-20 11:21:22
本文实例为大家分享了android使用入门第二十七篇点赞动画的具体代码,供大家参考,具体内容如下 mainactivity.java代码: package si...

本文实例为大家分享了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>

运行结果如图:

Android实现点赞动画(27)

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

上一篇:

下一篇: