Android自定义View图片按Path运动和旋转
作者:匆忙拥挤repeat 发布时间:2022-09-15 22:53:11
标签:Android,View,运动旋转
本文实例为大家分享了Android自定义View图片按Path运动旋转的具体代码,供大家参考,具体内容如下
View:
/**
* author : stone
* email : aa86799@163.com
* time : 16/5/29 15 29
*/
public class EarthPathView extends View {
private Path mPath;
private Paint mPaint;
private Bitmap mBitmap;
private PathMeasure mPathMeasure;
private float[] mPoint;
private float[] mTan;
private float mDdegrees;
public EarthPathView(Context context) {
this(context, null);
}
public EarthPathView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public EarthPathView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mPaint = new Paint();
mPaint.setColor(Color.RED);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(10);
InputStream is = getResources().openRawResource(R.drawable.earth);
mBitmap = BitmapFactory.decodeStream(is);
}
public void setPath(Path path) {
mPath = path;
mPathMeasure = new PathMeasure(path, false);
mPoint = new float[2];
mTan = new float[2];
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (mPath == null) {
return;
}
canvas.rotate(mDdegrees+=2, getWidth()/2, getHeight()/2);
canvas.drawPath(mPath, mPaint);
float degress = (float) Math.toDegrees(Math.atan2(mTan[1], mTan[0]));
Matrix matrix = new Matrix();
matrix.postRotate(degress, mBitmap.getWidth() / 2, mBitmap.getHeight() / 2);
matrix.postTranslate(mPoint[0] - mBitmap.getWidth() / 2, mPoint[1] - mBitmap.getHeight() / 2);
canvas.drawBitmap(mBitmap, matrix, null);
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public void startAnim() {
ValueAnimator animator = ValueAnimator.ofFloat(0, mPathMeasure.getLength());
animator.setDuration(2000);
animator.setInterpolator(new LinearInterpolator()); //插值器
animator.setRepeatCount(ValueAnimator.INFINITE);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
float distance = (float) animation.getAnimatedValue();
mPathMeasure.getPosTan(distance, mPoint, mTan);
invalidate();
}
});
animator.start();
}
}
Activity
package com.stone.canvaspath;
import android.app.Activity;
import android.graphics.Path;
import android.os.Bundle;
import com.stone.canvaspath.earth.EarthPathView;
/**
* author : stone
* email : aa86799@163.com
* time : 16/5/29 15 27
*/
public class EarthActivity extends Activity {
private EarthPathView mPathView;
private Path mPath;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int w = getResources().getDisplayMetrics().widthPixels;
int h = getResources().getDisplayMetrics().heightPixels;
mPathView = new EarthPathView(this);
setContentView(mPathView);
int min = Math.min(w, h);
buildPath(w / 2 + 100, h / 2 + 100, min / 4);
mPathView.setPath(mPath);
mPathView.startAnim();
}
private void buildPath(float x, float y, float radius) {
mPath = new Path();
mPath.addCircle(x, y, radius, Path.Direction.CW);
}
}
来源:http://blog.csdn.net/jjwwmlp456/article/details/52046767


猜你喜欢
- 什么是 Spring Boot 插件?Spring Boot 插件是一种扩展机制,它提供了一种简单的方式来扩展 Spring Boot 的功
- 有时安全不得不考虑,看看新闻泄漏风波事件就知道了我们在用Spring boot进行开发时,经常要配置很多外置参数ftp、数据库连接信息、支付
- 在本文中,笔者向大家介绍下Java中一个非常重要也非常有趣的特性,就是自动装箱与拆箱,并从源码中解读自动装箱与拆箱的原理,同时这种特性也留有
- springboot项目部署平时我们在部署springboot打成jar方式部署得时候,大多数都会编写启动脚本,脚本有很多种写法,但大多数意
- springMVC后台处理数组对象。list类型的参数,接收前台的数组值,实验了一下,结果还真可以。不用绑定到对象里面。当然我这个是前台传递
- 这里我们以拨打电话申请权限来写个小例子,也就是CALL_PHONE,因为拨打电话会涉及用户手机的资费问题,因而被列为了危险权限,在Andro
- 一、准备java我已经把java装到了在D盘:二、配置java环境变量点击设置,进入windows设置页面;搜索高级系统设置:在系统变量里添
- 本文实例讲述了C#使用GDI绘制直线的方法。分享给大家供大家参考。具体实现方法如下:Point p1=new Point(200,200);
- 1、使用AsyncTask异步任务实现,调用publishProgress()方法刷新进度来实现(已优化)public class MyAs
- 1.介绍当系统准备为用户提供一系列相关对象,又不想让用户代码和这些对象形成耦合时,就可以使用抽象工厂模式。2.如何实现1)抽象产品--Car
- 结构体是由一系列具有相同类型或不同类型的数据构成的数据集合。所以,标准C中的结构体是不允许包含成员函数的,当然C++中的结构体对此进行了扩展
- 本文实例讲述了C#修改IIS站点framework版本号的方法。分享给大家供大家参考。具体如下:使用ASP.NET IIS 注册工具 (As
- tcp客户端示例#include <errno.h> #include <sys/socket.h> #includ
- 本文实例讲述了WinForm实现移除控件某个事件的方法,供大家参考借鉴一下。具体功能代码如下:主要功能部分代码如下:/// <summ
- 在选择排序中,从第一个元素开始,依次遍历数组中的元素,找出当前遍历元素之后的最小元素,与当前遍历元素交换位置,依此类推,是一种由前往后的排序
- 并发编程中的三个概念:1.原子性在Java中,对基本数据类型的变量的读取和赋值操作是原子性操作,即这些操作是不可被中断的,要么执行,要么不执
- 虽然项目中都夹杂了Hibernate的支持,但是团队开发中,很多人为了编写特殊查询的代码时都使用了JDBC进行查询。JDBC查询后返回的是一
- 温馨提示:本教程的 GitHub 地址为「intellij-idea-tutorial」,欢迎感兴趣的童鞋Star、Fork,纠错。首先,给
- 一、缩略图在浏览相册的时候,可能需要生成相应的缩略图。直接上代码:public class ImageUtil { private Logg
- SpringBoot是什么?SpringBoot是spring家族中微型框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。