属性动画+自定义view进度圆环的倒计时
来源:互联网 发布:html设计软件 编辑:程序博客网 时间:2024/05/30 04:33
属性动画+自定义view进度圆环的倒计时
MainActivity
public class MainActivity extends AppCompatActivity { private ImageView iv; private Handler handler; private MyView myView; int item=3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); iv = (ImageView) findViewById(R.id.iv); myView= (MyView) findViewById(R.id.myView); //动画的方法 initAnimator(); } //属性动画 private void initAnimator() { AnimatorSet set = new AnimatorSet(); float y = iv.getTranslationY(); ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(iv,"translationY",y,450); objectAnimator.setDuration(3000); ObjectAnimator objectAnimator1 = ObjectAnimator.ofFloat(iv,"scaleY",1f,2f,1f); objectAnimator1.setDuration(3000); set.play(objectAnimator).before(objectAnimator1); set.start(); //倒计时跳转 handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { item--; if (item==1){ Intent intent=new Intent(MainActivity.this,ShowActivity.class); startActivity(intent); } handler.postDelayed(this,2000); } },2000); new AsyncTask<String,Integer,String>(){ @Override protected String doInBackground(String... strings) { for (int i = 1; i <=4 ; i++) { SystemClock.sleep(1000); publishProgress(i); } return null; } @Override protected void onProgressUpdate(Integer... values) { myView.setProgress(values[0]); } }.execute(); }}
自定义viewpackage com.example.myviewhandler;import android.animation.ArgbEvaluator;import android.animation.ValueAnimator;import android.content.Context;import android.content.res.TypedArray;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.RectF;import android.util.AttributeSet;import android.view.View;/** * 自定义View */public class MyView extends View { private Paint paint; boolean flag = true; private int mProgress = 0; private int mCountProgress = 0; private float mRadiuSize = 0; private float mRingSize = 0; private float mTextSize = 0; private int mProgressColor = 0; private int currentColor; private ValueAnimator animator; public MyView(Context context) { super(context); init(); } //自定义方法 private void init() { //画笔 paint=new Paint(); paint.setAntiAlias(true); } public MyView(Context context, AttributeSet attrs) { super(context, attrs); //调用下面定义的一个方法 getCustomAttr(context,attrs); init(); } public MyView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); getCustomAttr(context,attrs); init(); } //实现测量的方法 @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int widthmode = MeasureSpec.getMode(widthMeasureSpec); int widthsize = MeasureSpec.getSize(widthMeasureSpec); int hightmode = MeasureSpec.getMode(heightMeasureSpec); int hightsize = MeasureSpec.getSize(heightMeasureSpec); int width=0; int hight=0; if (widthmode==MeasureSpec.AT_MOST){ width = (int) (mRadiuSize *2); }else{ width= Math.max(widthsize,(int)(mRadiuSize*2)); } if (hightmode==MeasureSpec.AT_MOST){ hight = (int) (mRadiuSize * 2); }else{ hight= Math.max(hightsize,(int)(mRadiuSize*2)); } setMeasuredDimension(width,hight); } //自定义的一个方法 private void getCustomAttr(Context context, AttributeSet attrs) { TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.MyView); mRadiuSize = array.getDimension(R.styleable.MyView_radiuSize, 100); mRingSize = array.getDimension(R.styleable.MyView_ringSize, 10); mTextSize = array.getDimension(R.styleable.MyView_textSize, 10); mProgressColor = array.getColor(R.styleable.MyView_progressColor, currentColor); } @Override protected void onDraw(Canvas canvas) { // super.onDraw(canvas); animator = ValueAnimator.ofObject(new ArgbEvaluator(), Color.RED, Color.BLUE); animator.setDuration(500); animator.setRepeatCount(ValueAnimator.INFINITE); animator.setRepeatMode(ValueAnimator.REVERSE); animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator valueAnimator) { currentColor = (int) valueAnimator.getAnimatedValue(); } }); paint.setStrokeWidth(0); paint.setColor(Color.BLACK); paint.setStyle(Paint.Style.STROKE); canvas.drawCircle(getMeasuredWidth() / 2, getMeasuredHeight() / 2, mRadiuSize, paint); canvas.drawCircle(getMeasuredWidth() / 2, getMeasuredHeight() / 2, mRadiuSize - mRingSize, paint); paint.setTextSize(mTextSize); String text = mCountProgress+""; float textWidth = paint.measureText(text); canvas.drawText(text, getMeasuredWidth() / 2 - textWidth / 2, getMeasuredWidth() / 2 + mTextSize / 2, paint); RectF rectF = new RectF(getMeasuredWidth() / 2 - mRadiuSize + mRingSize / 2, getMeasuredHeight() / 2 - mRadiuSize + mRingSize / 2, getMeasuredWidth() / 2 + mRadiuSize - mRingSize / 2, getMeasuredHeight() / 2 + mRadiuSize - mRingSize / 2); paint.setStrokeWidth(mRingSize); paint.setColor(currentColor); canvas.drawArc(rectF, 0, mProgress*90, false, paint); } public void setProgress(int progress) { if (flag) { animator.start(); flag = false; } mProgress = (4-progress); mCountProgress = (4-progress) ; invalidate(); }}
跳转的第二个页面package com.example.myviewhandler;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;public class ShowActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_show); }}
要在values里创建attrs文件<?xml version="1.0" encoding="utf-8"?><resources> <declare-styleable name="MyView"> <attr name="progressColor" format="color"/> <attr name="textSize" format="dimension"/> <attr name="ringSize" format="dimension"/> <attr name="radiuSize" format="dimension"/> </declare-styleable></resources>
布局文件mainactivity
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" > <com.example.myviewhandler.MyView android:id="@+id/myView" android:layout_width="100dp" android:layout_height="100dp" app:ringSize="10dp" app:progressColor="#ff93" app:textSize="20sp" app:radiuSize="50dp" > </com.example.myviewhandler.MyView> <ImageView android:id="@+id/iv" android:layout_width="200dp" android:layout_height="200dp" android:src="@mipmap/y" android:scaleType="fitXY" android:layout_centerHorizontal="true" /></RelativeLayout>
showactivity 布局<?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" tools:context="com.example.myviewhandler.ShowActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="展示页面" /></RelativeLayout>
阅读全文
0 0
- 属性动画+自定义view进度圆环的倒计时
- Android自定义View和属性动画完美结合,创造出酷炫圆环动画,带标尺和进度
- 一个自定义圆环进度View
- 自定义view,实现圆环进度
- 自定义View实现进度圆环
- 自定义圆环view 倒计时控件
- 自定义控件,动画效果的进度圆环RingProgressBar
- Android 自定义View实例之进度圆环
- 自定义view圆环的改变
- 自定义view圆环跟随progressbar进度改变中心的texview(百分比)
- 自定义进度圆环
- 自定义圆环进度
- 自定义圆环倒计时
- 自定义圆环倒计时
- 自定义View,自定义属性(带进度的圆形进度条)
- Android自定义一个带缩放动画的倒计时View
- H5动画,canvas绘制圆环百分比进度的动态效果
- H5动画,canvas绘制圆环百分比进度的动态效果
- angular的仿淘宝购物车
- 关于慕课网上的Nginx 从入门到实践课程视频介绍
- 加快QT工程编译速度
- 安卓文件处理-文件压缩篇(不定时更新)
- 异常捕获
- 属性动画+自定义view进度圆环的倒计时
- mvp
- 异常捕获器
- 用户名查找和增删
- UVa10817
- 安卓App的备份与恢复-不定时更新
- java学习第34天,集合 迭代器
- angularjs-查询.删除,添加数据,修改密码
- 【状态设计的力量】【JZOJ 5411】 友谊