大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
这期内容当中小编将会给大家带来有关怎么在Android中使用SurfaceView实现一个点赞动画,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
成都创新互联公司网站建设公司,提供成都网站设计、网站制作,网页设计,建网站,PHP网站建设等专业做网站服务;可快速的进行网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,是专业的做网站团队,希望更多企业前来合作!
ZanBean类,每个ZanBean都要负责实时更新自己的位置、透明度等数据
import android.animation.TypeEvaluator; import android.animation.ValueAnimator; import android.annotation.TargetApi; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Matrix; import android.graphics.Paint; import android.graphics.Point; import android.os.Build; import java.util.Random; public class ZanBean { /** * 心的当前坐标 */ public Point point; /** * 移动动画 */ private ValueAnimator moveAnim; /** * 放大动画 */ private ValueAnimator zoomAnim; /** * 透明度 */ public int alpha = 255;// /** * 心图 */ private Bitmap bitmap; /** * 绘制bitmap的矩阵 用来做缩放和移动的 */ private Matrix matrix = new Matrix(); /** * 缩放系数 */ private float sf = 0; /** * 产生随机数 */ private Random random; public boolean isEnd = false;//是否结束 public ZanBean(Context context, int resId, ZanView zanView) { random = new Random(); bitmap = BitmapFactory.decodeResource(context.getResources(), resId); init(new Point(zanView.getWidth() / 2, zanView.getHeight()- bitmap.getHeight() / 2), new Point((random.nextInt(zanView.getWidth())), 0)); } public ZanBean(Bitmap bitmap, ZanView zanView) { random = new Random(); this.bitmap = bitmap; //为了让在起始坐标点时显示完整 需要减去bitmap.getHeight()/2 init(new Point(zanView.getWidth() / 2, zanView.getHeight() - bitmap.getHeight() / 2), new Point((random.nextInt(zanView.getWidth())), 0)); } @TargetApi(Build.VERSION_CODES.HONEYCOMB) private void init(final Point startPoint, Point endPoint) { moveAnim = ValueAnimator.ofObject(new BezierEvaluator(new Point(random.nextInt(startPoint.x * 2), Math.abs(endPoint.y - startPoint.y) / 2)), startPoint, endPoint); moveAnim.setDuration(1500); moveAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { point = (Point) animation.getAnimatedValue(); alpha = (int) ((float) point.y / (float) startPoint.y * 255); } }); moveAnim.start(); zoomAnim = ValueAnimator.ofFloat(0, 1f).setDuration(700); zoomAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { Float f = (Float) animation.getAnimatedValue(); sf = f.floatValue(); } }); zoomAnim.start(); } // public void pause(){ // if(moveAnim !=null&& moveAnim.isRunning()){ // moveAnim.pause(); // } // if(zoomAnim !=null&& zoomAnim.isRunning()){ // zoomAnim.pause(); // } // } // // public void resume(){ // if(moveAnim !=null&& moveAnim.isPaused()){ // moveAnim.resume(); // } // if(zoomAnim !=null&& zoomAnim.isPaused()){ // zoomAnim.resume(); // } // } public void stop() { if (moveAnim != null) { moveAnim.cancel(); moveAnim = null; } if (zoomAnim != null) { zoomAnim.cancel(); zoomAnim = null; } } /** * 主要绘制函数 */ public void draw(Canvas canvas, Paint p) { if (bitmap != null && alpha > 0) { p.setAlpha(alpha); matrix.setScale(sf, sf, bitmap.getWidth() / 2, bitmap.getHeight() / 2); matrix.postTranslate(point.x - bitmap.getWidth() / 2, point.y - bitmap.getHeight() / 2); canvas.drawBitmap(bitmap, matrix, p); } else { isEnd = true; } } /** * 二次贝塞尔曲线 */ @TargetApi(Build.VERSION_CODES.HONEYCOMB) private class BezierEvaluator implements TypeEvaluator{ private Point centerPoint; public BezierEvaluator(Point centerPoint) { this.centerPoint = centerPoint; } @Override public Point evaluate(float t, Point startValue, Point endValue) { int x = (int) ((1 - t) * (1 - t) * startValue.x + 2 * t * (1 - t) * centerPoint.x + t * t * endValue.x); int y = (int) ((1 - t) * (1 - t) * startValue.y + 2 * t * (1 - t) * centerPoint.y + t * t * endValue.y); return new Point(x, y); } } }
ZanView代码如下:SurfaceView,不断将ZanBean画到自己的画布上。
import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.PixelFormat; import android.graphics.PorterDuff; import android.util.AttributeSet; import android.view.SurfaceHolder; import android.view.SurfaceView; import java.util.ArrayList; public class ZanView extends SurfaceView implements SurfaceHolder.Callback { private SurfaceHolder surfaceHolder; /** * 心的个数 */ private ArrayListzanBeen = new ArrayList<>(); private Paint p; /** * 负责绘制的工作线程 */ private DrawThread drawThread; public ZanView(Context context) { this(context, null); } public ZanView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public ZanView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); this.setZOrderOnTop(true); /**设置画布 背景透明*/ this.getHolder().setFormat(PixelFormat.TRANSLUCENT); surfaceHolder = getHolder(); surfaceHolder.addCallback(this); p = new Paint(); p.setAntiAlias(true); drawThread = new DrawThread(); } /** * 点赞动作 添加心的函数 控制画面最大心的个数 */ public void addZanXin(ZanBean zanBean) { zanBeen.add(zanBean); if (zanBeen.size() > 40) { zanBeen.remove(0); } start(); } @Override public void surfaceCreated(SurfaceHolder holder) { if (drawThread == null) { drawThread = new DrawThread(); } drawThread.start(); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } @Override public void surfaceDestroyed(SurfaceHolder holder) { if (drawThread != null) { drawThread.isRun = false; drawThread = null; } } class DrawThread extends Thread { boolean isRun = true; @Override public void run() { super.run(); /**绘制的线程 死循环 不断的跑动*/ while (isRun) { Canvas canvas = null; try { synchronized (surfaceHolder) { canvas = surfaceHolder.lockCanvas(); /**清除画面*/ canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR); boolean isEnd = true; /**对所有心进行遍历绘制*/ for (int i = 0; i < zanBeen.size(); i++) { isEnd = zanBeen.get(i).isEnd; zanBeen.get(i).draw(canvas, p); } /**这里做一个性能优化的动作,由于线程是死循环的 在没有心需要的绘制的时候会结束线程*/ if (isEnd) { isRun = false; drawThread = null; } } } catch (Exception e) { e.printStackTrace(); } finally { if (canvas != null) { surfaceHolder.unlockCanvasAndPost(canvas); } } try { /**用于控制绘制帧率*/ Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } } } } public void stop() { if (drawThread != null) { // for (int i = 0; i < zanBeen.size(); i++) { // zanBeen.get(i).pause(); // } for (int i = 0; i < zanBeen.size(); i++) { zanBeen.get(i).stop(); } drawThread.isRun = false; drawThread = null; } } public void start() { if (drawThread == null) { // for (int i = 0; i < zanBeen.size(); i++) { // zanBeen.get(i).resume(); // } drawThread = new DrawThread(); drawThread.start(); } } }
调用方式:
public class TestActivity extends BaseActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test_zan); final ZanView zan = (ZanView) findViewById(R.id.zan_view); zan.start(); findViewById(R.id.button).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ZanBean zanBean = new ZanBean(BitmapFactory.decodeResource(getResources(), R.drawable.ic_default_avatar), zan); zan.addZanXin(zanBean); } }); } }
上述就是小编为大家分享的怎么在Android中使用SurfaceView实现一个点赞动画了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注创新互联行业资讯频道。