使用场景:在某一页面布局中,除了原生View还有自定义View,当触发自定义View的击事件时,该页面的原生布局需要同步进行处理
demo:主页面有两部分UI,一部分是原生的UI绘制的简易糖葫芦,另一部分是自定义View绘制的圆,当点击该圆时,随机产生1-3的随机数,圆对应的色块高亮,且原生布局对应的糖葫芦消失。
下图:点击自定义圆时,随机数为2,紫色高亮,糖葫芦中紫色消失
同理,若随机数为1:
若随机数为3:
------------------------------------------分割线--------------------------------------------
【1】抽象出方法
【2】前者调用实例化对象
【3】在需要的地方调用listener
【4】前者方法实现
------------------------------------------分割线--------------------------------------------
UI:
Activity:
package com.tzbc.databindtest;//此处应为自己项目的包
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import com.tzbc.databindtest.Listener.RoundListener;
import com.tzbc.databindtest.databinding.ActivityMainBinding;
public class MainActivity extends AppCompatActivity implements RoundListener {
private static final String TAG = "tzbc";
private static final int PINK = 1;
private static final int PURPLE = 2;
private static final int ORANGE = 3;
private TestVM vm;
private ActivityMainBinding activityMainBinding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
vm = new TestVM();
activityMainBinding.setVm(vm);
activityMainBinding.rvRound.resetDrawHigh();
activityMainBinding.rvRound.setRoundListener(this);
}
@Override
public void eatRound(int eatIndex) {
Log.e(TAG, "即将吃掉糖葫芦...");
switch (eatIndex){
case PINK:
activityMainBinding.ivRoundPink.setVisibility(View.INVISIBLE);
break;
case PURPLE:
activityMainBinding.ivRoundPurple.setVisibility(View.INVISIBLE);
break;
case ORANGE:
activityMainBinding.ivRoundOrange.setVisibility(View.INVISIBLE);
break;
}
Log.e(TAG, "已经吃掉糖葫芦!");
}
@Override
public void resetRound() {
Log.e(TAG, "正在重置糖葫芦...");
resetRoundVisibility();
}
private void resetRoundVisibility(){
activityMainBinding.ivRoundPink.setVisibility(View.VISIBLE);
activityMainBinding.ivRoundPurple.setVisibility(View.VISIBLE);
activityMainBinding.ivRoundOrange.setVisibility(View.VISIBLE);
}
}
自定义View:
package com.tzbc.databindtest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import com.tzbc.databindtest.Listener.RoundListener;
import java.util.Random;
/**
* Created by 桃子不出 on 2020/4/15.
*
* @author tzbc
*/
public class RoundView extends View {
private static final String TAG = "RoundView";
private RoundListener roundListener;
private int width, height;
private Paint paintPink, paintPurple, paintOrange;
private Paint paintPinkHigh, paintPurpleHigh, paintOrangeHigh;
private boolean drawHighPink = false;
private boolean drawHighOrange = false;
private boolean drawHighPurple = false;
public RoundView(Context context) {
super(context);
init();
}
public RoundView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
paintPink = new Paint();
paintPink.setColor(Color.parseColor("#DF7D9E"));
paintPink.setStyle(Paint.Style.FILL);
paintPink.setAntiAlias(true);
paintPurple = new Paint();
paintPurple.setColor(Color.parseColor("#D05AE6"));
paintPurple.setStyle(Paint.Style.FILL);
paintPurple.setAntiAlias(true);
paintOrange = new Paint();
paintOrange.setColor(Color.parseColor("#E97A70"));
paintOrange.setStyle(Paint.Style.FILL);
paintOrange.setAntiAlias(true);
//用于高亮部分
paintPinkHigh = new Paint();
paintPinkHigh.setColor(Color.parseColor("#F33D7A"));
paintPinkHigh.setStyle(Paint.Style.FILL);
paintPinkHigh.setAntiAlias(true);
paintPurpleHigh = new Paint();
paintPurpleHigh.setColor(Color.parseColor("#A216BD"));
paintPurpleHigh.setStyle(Paint.Style.FILL);
paintPurpleHigh.setAntiAlias(true);
paintOrangeHigh = new Paint();
paintOrangeHigh.setColor(Color.parseColor("#F04D3E"));
paintOrangeHigh.setStyle(Paint.Style.FILL);
paintOrangeHigh.setAntiAlias(true);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
width = getMeasuredWidth();
height = getMeasuredHeight();
}
@SuppressLint("NewApi")
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawArc(0, 0, width, height, 270, 120, true, paintPink);
canvas.drawArc(0, 0, width, height, 30, 120, true, paintPurple);
canvas.drawArc(0, 0, width, height, 150, 120, true, paintOrange);
if (drawHighPink) {
canvas.drawArc(0, 0, width, height, 270, 120, true, paintPinkHigh);
} else if (drawHighPurple) {
canvas.drawArc(0, 0, width, height, 30, 120, true, paintPurpleHigh);
} else if (drawHighOrange) {
canvas.drawArc(0, 0, width, height, 150, 120, true, paintOrangeHigh);
}
}
public void setRoundListener(RoundListener listener) {
this.roundListener = listener;
}
public void resetDrawHigh() {
drawHighPink = false;
drawHighPurple = false;
drawHighOrange = false;
if (roundListener != null) {
roundListener.resetRound();
}
}
private int getRandom() {
Random random = new Random();
return random.nextInt(3) % (3 - 1 + 1) + 1;
}
private void updateRandom(int index) {
resetDrawHigh();
switch (index) {
case 1:
drawHighPink = true;
break;
case 2:
drawHighPurple = true;
break;
case 3:
drawHighOrange = true;
break;
}
invalidate();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int randomIndex = getRandom();
Log.e(TAG, "被选中的糖葫芦为 : " + randomIndex);
updateRandom(randomIndex);
if (roundListener != null) {
roundListener.eatRound(randomIndex);
}
return super.onTouchEvent(event);
}
}
Listener:
package com.tzbc.databindtest.Listener;
/**
* Created by 桃子不出 on 2020/4/15.
*
* @author tzbc
*/
public interface RoundListener {
/**
* 吃掉的第几颗糖葫芦
*
* @param eatIndex eatIndex
*/
void eatRound(int eatIndex);
/**
* 重置糖葫芦
*/
void resetRound();
}