自定义圆行点击(颜色也变)
来源:互联网 发布:甘肃网络机柜 编辑:程序博客网 时间:2024/04/27 23:14
效果图
布局中:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<com.example.administrator.yn_yuan.CustomView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/c_view"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/c_text"
android:textSize="20sp"
android:layout_centerInParent="true"
android:text="ewrfewt"
android:layout_marginTop="10dp"/>
</RelativeLayout>
自定义圆
public class Custom_View extends View{
private SetOnWhat sow;
private int one;
private int two;
private int three;
private int RX;
private int RY;
private float width;
private int rect;
public Custom_View(Context context) {
super(context);
}
public Custom_View(Context context, AttributeSet attrs) {
super(context, attrs);
}
public Custom_View(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int x = getWidth();
int y = getHeight();
rect = x*3/4;
int R = rect*8/25;
width = R*3/8;
RX = x/2;
RY = y/2;
int r = rect/2 - (int) (width*2);
one = r;
two = (int) (r+width);
three = (int)(r+width*2);
int left = (x - rect)/2;
int right = left + rect;
int top = (y - rect)/2;
int bottom = top + rect;
Paint paint = new Paint();
paint.setColor(Color.GREEN);
paint.setStrokeWidth(width);
paint.setAntiAlias(true);
canvas.drawRect(left, top, right, bottom, paint);
paint.setColor(Color.WHITE);
canvas.drawCircle(RX, RY, r+width, paint);
paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.YELLOW);
canvas.drawCircle(RX, RY, R+width, paint);
// paint.setColor(Color.BLUE);
//
// canvas.drawCircle(RX, RY, R+width, paint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
float rawX = event.getRawX();
float rawY = event.getRawY();
int action = event.getAction();
if(action==MotionEvent.ACTION_DOWN||action==MotionEvent.ACTION_MOVE){
if (x + getLeft() < getRight() && y + getTop() < getBottom()) {
int type = 0;
int distance = getDistance(x, y);
if(distance<=one*one){
type = 0;
}else if(distance>one&&distance<=two*two){
type = 1;
}else if(distance>two&&distance<=three*three){
type = 2;
}else if(x>=(RX - rect/2)&&x<=(RX + rect/2)&&y>=(RY - rect/2)&&y<=(RY+rect/2)){
type = 3;
}else {
type = 4;
}
if(sow!=null){
sow.onWhat(type);
}
}
}
return true;
}
private int getDistance(int x, int y){
return (x-RX)*(x-RX) + (y-RY)*(y-RY);
}
public interface SetOnWhat{
void onWhat(int type);
}
public void getOnWhat(SetOnWhat sow){
this.sow = sow;
}
}
activity类
public class MainActivity extends AppCompatActivity {
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
CustomView cv = (CustomView) findViewById(R.id.c_view);
tv = (TextView) findViewById(R.id.c_text);
cv.getOnWhat(new CustomView.SetOnWhat() {
@Override
public void onWhat(int type) {
switch (type) {
case 0:
setText("这里是白色");
break;
case 1:
setText("这里是白色");
break;
case 2:
setText("这里是黄色");
break;
case 3:
setText("这里是绿色");
break;
case 4:
Intent intent =new Intent(MainActivity.this,NextActivity.class);
startActivity(intent);
break;
default:
break;
}
}
});
}
private void setText(String str){
tv.setText(str);
}
}
- 自定义圆行点击(颜色也变)
- gridview 点击行变颜色
- jquery点击table行 变颜色
- Button按钮点击变颜色
- 自定义View点击变字母
- 无需自定义UITabbar也可改变UITabbarController的背景和点击和的颜色
- iOS_自定义view里的按钮点击不了+背景颜色也设置不了
- jquery:点击之后变颜色,点击另一个,另一各变颜色,当前的恢复颜色
- listview 点击 item 字体变颜色
- tableview点击变cell背景颜色问题
- [自用门户]点击菜单变颜色
- 表格行变颜色
- cxGrid 行变颜色
- cxGrid 行变颜色
- ListView 点击颜色自定义设置
- IOS总结_无需自定义UITabbar也可改变UITabbarController的背景和点击和的颜色
- 一段点击后使td变颜色的代码
- JQuery实现点击文本框等时背景变颜色
- 数组元素的指针 vs 数组的指针
- NUC1015 计算数字的根
- python_使用matplotlib画柱状图(bar),散点图(scatter)
- 利用条件运算符的嵌套来完成此题:学习成绩>=90分的同学用A表示, 60-89分之间的用B表示,60分以下的用C表示
- hdu1281.cpp(匈牙利算法)
- 自定义圆行点击(颜色也变)
- 2016 计蒜之道 复赛 百度地图的实时路况 [Floyd][分治]
- Spark之WordCount
- 读程序 最大公约数
- 【Java学习笔记】17:Single Threaded Execution Pattern
- spark的排错和调优
- 欧拉定理
- cssselectVSxpath
- Leetcode 7 Reverse Integer