自定义View球、渐变、旋转

博客介绍了自定义View的实现步骤,首先要在values下编写attrs.xml文件,接着自定义一个View并继承View,然后涉及activity_main.xml,最后是MainActivity.java的相关操作。

第一步:在values下写attrs.xml文件

   <declare-styleable name="MyView">
         <attr name="myText" format="string"></attr>
         <attr name="myTextColor" format="color"></attr>
         <attr name="myTextSize" format="dimension"></attr>
    </declare-styleable>

第二步:自定义一个View并继承View:

public class MyView extends View {

    Paint paint=new Paint();
    String text;
    int textColor;
    float textSize;
    int w;
    int h;
    int x=0;

    public MyView(Context context) {
        super(context);
    }

    public MyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        paint.setColor(Color.RED);
        paint.setStrokeWidth(20);
        paint.setStyle(Paint.Style.STROKE);
        paint.setAntiAlias(true);

        TypedArray typedArray=context.obtainStyledAttributes(attrs, R.styleable.MyView);
       text=typedArray.getString(R.styleable.MyView_myText);
       textColor= typedArray.getColor(R.styleable.MyView_myTextColor,Color.BLACK);
       textSize=typedArray.getDimension(R.styleable.MyView_myTextSize,20f);
       typedArray.recycle();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        w=getMeasuredWidth();
        h=getMeasuredHeight();
    }
    boolean isgoing=true;

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawCircle(100,100,50,paint);
        if (isgoing) {
        }
        }
        ValueAnimator valueAnimator;
        private void startAnimation(){
            isgoing=false;
            valueAnimator=ValueAnimator.ofInt(0,w);
            valueAnimator.setDuration(3000);
            valueAnimator.setRepeatCount(ValueAnimator.INFINITE);
            valueAnimator.setInterpolator(new LinearInterpolator());
            valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    animation.getAnimatedValue();
                    postInvalidate();
                }
            });
            valueAnimator.start();
        }

}

第三步:activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:qq="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="废物"
        android:textSize="20sp"/>

    <com.example.lian0322_2.coust.MyView
        android:id="@+id/myview"
        android:layout_width="300dp"
        android:layout_height="300dp"
        qq:myText="要写的内容"
        qq:myTextColor="#00ff00"
        qq:myTextSize="20sp"
        />
</LinearLayout>

第四步:MainActivity.java

public class MainActivity extends AppCompatActivity {

    MyView myview;
    Button button;

    ObjectAnimator objectAnimator1;
    ObjectAnimator objectAnimator2;
    ObjectAnimator objectAnimator3;
    ObjectAnimator objectAnimator4;
    ObjectAnimator objectAnimator5;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        myview=findViewById(R.id.myview);
        button=findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                objectAnimator1=ObjectAnimator.ofFloat(myview,"translationX",0,300,0);
                objectAnimator2=ObjectAnimator.ofFloat(myview,"rotation",0,300,0);
                objectAnimator3=ObjectAnimator.ofFloat(myview,"scaleX",1,3,1);
                objectAnimator4=ObjectAnimator.ofFloat(myview,"scaleY",1,3,1);
                objectAnimator5 = ObjectAnimator.ofFloat(myview , "alpha" , 1 , 0 , 1);

                AnimatorSet animatorSet=new AnimatorSet();
                animatorSet.play(objectAnimator1).with(objectAnimator2).with(objectAnimator3).with(objectAnimator4).with(objectAnimator5);
                animatorSet.setDuration(3000);
                animatorSet.start();

            }
        });
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值