第一步:在value里面写attrs.xml
<declare-styleable name="MyTextView2">
<attr name="myText" format="string"/>
<attr name="myTextColor" format="color"/>
<attr name="myTextSize" format="dimension"/>
</declare-styleable>
第二步:自定义View并继承View
package com.example.lian0324.activity;
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.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;
import com.example.lian0324.R;
/**
* <p>文件描述:<p>
* <p>作者:董少博<p>
* <p>创建时间:2019/3/7<p>
* <p>更改时间:2019/3/7<p>
* <p>版本号:1<p>
*/
public class QiuView extends View {
Paint paint=new Paint();
int w,h;
String myText;
int myTextColor;
float myTextSize;
public QiuView(Context context) {
super(context);
}
public QiuView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
paint.setColor(Color.RED);
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.FILL);
paint.setStrokeWidth(20);
TypedArray typedArray=context.obtainStyledAttributes(attrs, R.styleable.MyTextView2);
myText=typedArray.getString(R.styleable.MyTextView2_myText);
myTextColor=typedArray.getColor(R.styleable.MyTextView2_myTextColor,Color.BLACK);
myTextSize=typedArray.getDimension(R.styleable.MyTextView2_myTextSize,30f);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(100,100,60,paint);
if (isgong) {
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
w=getMeasuredWidth();
h=getMeasuredHeight();
}
boolean isgong=true;
ValueAnimator valueAnimator;
private void startAnimator(){
isgong=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();
}
}
第三步:写布局:
<com.example.lian0324.activity.QiuView
android:id="@+id/myqiu"
android:layout_width="match_parent"
android:layout_height="match_parent"
qq:myText="要写的内容"
qq:myTextColor="#00ff00"
qq:myTextSize="25sp" />
第四步:在Activity里面写并继承AppCompatActivity:
QiuView qiuView;
ObjectAnimator objectAnimator;
ObjectAnimator objectAnimator1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
qiuView=findViewById(R.id.myqiu);
objectAnimator=ObjectAnimator.ofFloat(qiuView,"translationX",0,1000);
objectAnimator1=ObjectAnimator.ofFloat(qiuView,"translationY",0,1700);
AnimatorSet animatorSet=new AnimatorSet();
animatorSet.setDuration(3000);
animatorSet.play(objectAnimator).with(objectAnimator1);
animatorSet.start();
animatorSet.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
startActivity(new Intent(MainActivity.this,LoginActivity.class));
finish();
}
});
}
}