第一步:在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();
}
});
}
}