Android studio动画

来源:互联网 发布:linux 杀死进程 编辑:程序博客网 时间:2024/06/06 12:45

public class MainActivity extends AppCompatActivity {View view;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        view = (View)findViewById(R.id.view);        ObjectAnimator scale1 = ObjectAnimator.ofFloat(view,"translationX",0,400f);        ObjectAnimator scale2 = ObjectAnimator.ofFloat(view,"translationY",0,450f);        AnimatorSet animset = new AnimatorSet();        animset.play(scale1).with(scale2);        animset.setDuration(3000);        animset.start();        animset.addListener(new Animator.AnimatorListener() {


//对应相关布局来进行调用动画 和自定义View

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    xmlns:dabin="http://schemas.android.com/apk/res-auto"    android:id="@+id/activity_main"    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="com.eightgroup.zk3lx.MainActivity">    <ImageView        android:id="@+id/imageView"        android:layout_width="wrap_content"        android:layout_height="wrap_content"       android:src="@mipmap/ic_launcher"        />    <com.eightgroup.zk3lx.Myview         android:layout_marginLeft="100dp"        android:id="@+id/my_view"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        dabin:stripeWidth="15dp"        dabin:centerTextSize="16sp"        dabin:percent="3" />    <Button        android:id="@+id/start"        android:text="Go"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginLeft="26dp"        android:layout_marginStart="26dp"        android:layout_marginBottom="117dp"        android:layout_alignParentBottom="true"        android:layout_alignParentLeft="true"        android:background="@color/colorAccent"        android:layout_alignParentStart="true" /></RelativeLayout>

//
public class MainActivity extends AppCompatActivity {     ImageView imageView;    private Myview myView;    private Button start;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);          myView = (Myview)findViewById(R.id.my_view);          start = (Button)findViewById(R.id.start);        imageView = (ImageView) findViewById(R.id.imageView);        ObjectAnimator move = ObjectAnimator.ofFloat(imageView,"translationY",0,300f);        ObjectAnimator scale1 = ObjectAnimator.ofFloat(imageView,"scaleX",1f,2f,1f);        ObjectAnimator scale2 = ObjectAnimator.ofFloat(imageView,"scaleY",1f,2f,1f);        AnimatorSet animset = new AnimatorSet();        animset.play(scale1).with(scale2).after(move);        animset.setDuration(3000);        animset.start();        //动画的监听        animset.addListener(new Animator.AnimatorListener() {            //动画开始            @Override            public void onAnimationStart(Animator animator) {            }            //动画结束            @Override            public void onAnimationEnd(Animator animator) {                Toast.makeText(MainActivity.this, "结束", Toast.LENGTH_SHORT).show();                Intent intent = new Intent(MainActivity.this,Main2Activity.class);                startActivity(intent);            }            @Override            public void onAnimationCancel(Animator animator) {            }            @Override            public void onAnimationRepeat(Animator animator) {            }        });        //开始        start.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                int n = (int) (Math.random() * 100);                myView.setPercent(n);            }        });    }}
//创建View 类   
public class Myview extends View {    //圆的半径    private float mRadius;    //色带的宽度    private float mStripeWidth;    //总体大小    private int mHeight;    private int mWidth;    //动画位置百分比进度    private int mCurPercent;    //实际百分比进度    private int mPercent;    //圆心坐标    private float x;    private float y;    //要画的弧度    private int mEndAngle;    //小圆的颜色    private int mSmallColor;    //大圆颜色    private int mBigColor;    //中心百分比文字大小    private float mCenterTextSize;    public Myview(Context context) {        this(context, null);    }    public Myview(Context context, AttributeSet attrs) {        this(context, attrs, 0);    }    public Myview(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        // 获取自定义属性        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Myview, defStyleAttr, 0);        //获取色带的宽度        mStripeWidth = a.getDimension(R.styleable.Myview_stripeWidth, PxUtils.dpToPx(30, context));        //获取当前的百分比        mCurPercent = a.getInteger(R.styleable.Myview_percent, 0);        //获取小园的颜色        mSmallColor = a.getColor(R.styleable.Myview_smallColor, Color.BLACK);        //获取大圆的颜色        mBigColor = a.getColor(R.styleable.Myview_bigColor, Color.WHITE);        //获取中心文字的大小        mCenterTextSize = a.getDimensionPixelSize(R.styleable.Myview_centerTextSize, PxUtils.spToPx(50, context));        //获取园的半径        mRadius = a.getDimensionPixelSize(R.styleable.Myview_radius, PxUtils.dpToPx(100, context));    }    //测量    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        //获取测量模式        int widthMode = MeasureSpec.getMode(widthMeasureSpec);        int heightMode = MeasureSpec.getMode(heightMeasureSpec);        //获取测量大小        int widthSize = MeasureSpec.getSize(widthMeasureSpec);        int heightSize = MeasureSpec.getSize(heightMeasureSpec);        //如果为确定大小值,则圆的半径为宽度/2        if (widthMode == MeasureSpec.EXACTLY && heightMode == MeasureSpec.EXACTLY) {            mRadius = widthSize / 2;            x = widthSize / 2;            y = heightSize / 2;            mWidth = widthSize;            mHeight = heightSize;        }        //如果为wrap_content 那么View大小为圆的半径大小*2        if (widthMode == MeasureSpec.AT_MOST && heightMode == MeasureSpec.AT_MOST) {            mWidth = (int) (mRadius * 2);            mHeight = (int) (mRadius * 2);            x = mRadius;            y = mRadius;        }        //设置视图的大小        super.onMeasure(widthMeasureSpec, heightMeasureSpec);    }    @Override    protected void onDraw(Canvas canvas) {        mEndAngle = (int) (mCurPercent * 3.6);        //绘制大圆        Paint bigCirclePaint = new Paint();        bigCirclePaint.setAntiAlias(true);        bigCirclePaint.setColor(mBigColor);        canvas.drawCircle(x, y, mRadius, bigCirclePaint);        //饼状图        Paint sectorPaint = new Paint();        sectorPaint.setColor(mSmallColor);        sectorPaint.setAntiAlias(true);        RectF rect = new RectF(0, 0, mWidth, mHeight);        canvas.drawArc(rect, 270, mEndAngle, true, sectorPaint);        //绘制小圆,颜色透明        Paint smallCirclePaint = new Paint();        smallCirclePaint.setAntiAlias(true);        smallCirclePaint.setColor(mBigColor);        canvas.drawCircle(x, y, mRadius - mStripeWidth, smallCirclePaint);        //绘制文本        Paint textPaint = new Paint();        String text = mCurPercent + "%";        textPaint.setTextSize(mCenterTextSize);        float textLength = textPaint.measureText(text);        textPaint.setColor(Color.RED);        canvas.drawText(text, x - textLength / 2, y, textPaint);        super.onDraw(canvas);    }    public void setmSmallColor(int mSmallColor) {        this.mSmallColor = mSmallColor;        invalidate();    }    //重置    public void setReset(int percent) {        mCurPercent = percent;        invalidate();    }    //外部设置百分比数    public void setPercent(int percent) {        if (percent > 100) {            throw new IllegalArgumentException("percent must less than 100!");        }        setCurPercent(percent);    }    //内部设置百分比 用于动画效果    private void setCurPercent(int percent) {        new Thread(new Runnable() {            @Override            public void run() {                for (int i = 0; i < 100; i++) {                    mCurPercent = i;                    try {                        Thread.sleep(40);                    } catch (InterruptedException e) {                        e.printStackTrace();                    }                    postInvalidate();                }            }        }).start();    }}
//应用类
public class PxUtils {    public static int dpToPx(int dp, Context context) {        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources().getDisplayMetrics());    }    public static int spToPx(int sp,Context context) {        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, context.getResources().getDisplayMetrics());    }}
//对应的Value 的应用    下自定义一个attrs.xml,

<?xml version="1.0" encoding="utf-8"?>    <resources>        <declare-styleable name="Myview">            <attr name="radius" format="dimension"/>            <attr name="stripeWidth" format="dimension"/>   <!--色带宽度-->            <attr name="percent" format="integer"/>         <!--百分比 最大值为100-->            <attr name="smallColor" format="color"/>        <!--色带宽度-->            <!--外圈颜色-->            <attr name="bigColor" format="color"/>            <!--中间字体颜色-->            <attr name="centerTextSize" format="dimension"/>            <!--色带宽度-->        </declare-styleable></resources>


public class View extends android.view.View {    public View(Context context) {        super(context);    }    public View(Context context, AttributeSet attrs) {        super(context, attrs);    }    public View(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);    }    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        Paint paint =new Paint();        paint.setColor(Color.BLUE);        canvas.drawCircle(30,30,30,paint);    }}



原创粉丝点击