1.
自定义View的作用:
系统提供的一些控件,满足不了需求,由此出现了自定义View
(1.自定义view实现倒计时功能)
package com.bw.mydonghualianxi;
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;
import androidx.annotation.Nullable;
public class MyProgressView extends View {
private Paint paint;//定义一个画笔
private Paint paint2;
private Integer raduis;
private Integer numm;
private int anInt;
public MyProgressView(Context context) {
super(context);
initpaint();
}
public MyProgressView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyProgressView);
anInt = typedArray.getInt(R.styleable.MyProgressView_mytime, 5);
typedArray.recycle();
initpaint();
}
private void initpaint() {
paint=new Paint();
paint.setAntiAlias(true);//抗锯齿,润滑
paint.setDither(true);//抗抖动
paint.setStrokeWidth(10);//宽度
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.STROKE);
paint2=new Paint();
paint2.setAntiAlias(true);//抗锯齿 润滑
paint2.setDither(true);//抗抖动
paint2.setTextSize(20);
paint2.setColor(Color.RED);
paint2.setStyle(Paint.Style.FILL);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
RectF rectF = new RectF(20,20,150,150);
canvas.drawArc(rectF,0,raduis,false,paint);
canvas.drawText(numm+"",85,85,paint2);
}
public void starat() {
//圆弧
{
ValueAnimator valueAnimator = ValueAnimator.ofInt(0, 360);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
raduis = (Integer) valueAnimator.getAnimatedValue();
invalidate();
}
});
valueAnimator.setDuration(1000*anInt);
valueAnimator.start();
}
//倒计时文字
{
ValueAnimator valueAnimator = ValueAnimator.ofInt(anInt, 0);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
numm = (Integer) valueAnimator.getAnimatedValue();
invalidate();
}
});
valueAnimator.setDuration(1000*anInt);
valueAnimator.start();
}
}
}
在xml中调用
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DaoJiShiActivity">
<com.bw.mydonghualianxi.MyProgressView
app:mytime="3"
android:id="@+id/Mypass"
android:layout_width="200dp"
android:layout_height="200dp">
</com.bw.mydonghualianxi.MyProgressView>
</LinearLayout>
(2.自定义实现点赞)
package com.bw.mydonghualianxi;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.animation.CycleInterpolator;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class MyHeartView extends RelativeLayout {
public MyHeartView(Context context) {
super(context);
}
public MyHeartView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();//事件的类型:按下 移动 抬起
float x = event.getX();//获取位置
float y = event.getY();
if (MotionEvent.ACTION_DOWN==action){//判断是按下事件
addImagerView(x,y);
}
return true;
}
private void addImagerView(float x, float y) {
ImageView imageView = new ImageView(getContext());
imageView.setImageResource(R.mipmap.ic_launcher);
LayoutParams layoutParams = new LayoutParams(200, 200);//设置图片的大小,宽高
layoutParams.leftMargin= (int) x-100;//设置图片的位置
layoutParams.topMargin= (int) y-100;
imageView.setLayoutParams(layoutParams);
ValueAnimator valueAnimator = ObjectAnimator.ofFloat(imageView,"alpha",1,0);
ValueAnimator valueAnimator2 = ObjectAnimator.ofFloat(imageView,"translationY",0,-100);
ValueAnimator valueAnimator3 = ObjectAnimator.ofFloat(imageView,"rotation",0,30);
valueAnimator3.setInterpolator(new CycleInterpolator(2));
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.play(valueAnimator)
.with(valueAnimator2)
.with(valueAnimator3);
animatorSet.setDuration(2000);
animatorSet.start();
addView(imageView);
}
}
在xml中调用
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DianZanActivity">
<com.bw.mydonghualianxi.MyHeartView
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.bw.mydonghualianxi.MyHeartView>
</LinearLayout>
2.
自定义属性:
需要在values中创建一个资源文件用来存放设置自定义属性
如何获得自定义view
public MyProgressView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyProgressView);
anInt = typedArray.getInt(R.styleable.MyProgressView_mytime, 5);//(5)如果在xml中没有设置了这个属性,那就使用默认5
typedArray.recycle();
initpaint();
}