android系统中覆盖界面的设计
android系统中覆盖界面的设计,android,user-interface,Android,User Interface,我想为一个包含3个按钮(每个按钮都是一张卡片)的活动设计一个布局,如下所示: 我知道我可以用FrameLayout来设计它,如果是这样的话,我就不能处理每一张“卡片”一次点击事件,因为在FrameLayout中,最顶层是可触摸的。 有什么想法吗? 提前谢谢。我给你一个小主意如何处理这个问题。您可以根据自己的要求进行设计 这是密码 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" andr
我想为一个包含3个按钮(每个按钮都是一张卡片)的活动设计一个布局,如下所示:
我知道我可以用FrameLayout来设计它,如果是这样的话,我就不能处理每一张“卡片”一次点击事件,因为在FrameLayout中,最顶层是可触摸的。
有什么想法吗?
提前谢谢。我给你一个小主意如何处理这个问题。您可以根据自己的要求进行设计 这是密码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:padding="3dp" >
<ImageView
android:id="@+id/test_button_image"
android:layout_width="300dp"
android:layout_height="250dp"
android:layout_alignParentTop="true"
android:contentDescription="@string/app_name"
android:scaleType="fitXY"
android:src="@drawable/cardd2" >
</ImageView>
<ImageView
android:id="@+id/test_button_image1"
android:layout_width="250dp"
android:layout_height="270dp"
android:layout_alignParentTop="true"
android:contentDescription="@string/app_name"
android:scaleType="fitXY"
android:src="@drawable/cardd2" >
</ImageView>
<ImageView
android:id="@+id/test_button_image2"
android:layout_width="200dp"
android:layout_height="290dp"
android:layout_alignParentTop="true"
android:contentDescription="@string/app_name"
android:scaleType="fitXY"
android:src="@drawable/cardd2" >
</ImageView>
这是输出
根据您的需求进行设计
如果您想执行与这些功能不同的功能,还可以在“全部”上获得“单击事件”我将为您介绍如何使用这些功能。您可以根据自己的要求进行设计 这是密码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:padding="3dp" >
<ImageView
android:id="@+id/test_button_image"
android:layout_width="300dp"
android:layout_height="250dp"
android:layout_alignParentTop="true"
android:contentDescription="@string/app_name"
android:scaleType="fitXY"
android:src="@drawable/cardd2" >
</ImageView>
<ImageView
android:id="@+id/test_button_image1"
android:layout_width="250dp"
android:layout_height="270dp"
android:layout_alignParentTop="true"
android:contentDescription="@string/app_name"
android:scaleType="fitXY"
android:src="@drawable/cardd2" >
</ImageView>
<ImageView
android:id="@+id/test_button_image2"
android:layout_width="200dp"
android:layout_height="290dp"
android:layout_alignParentTop="true"
android:contentDescription="@string/app_name"
android:scaleType="fitXY"
android:src="@drawable/cardd2" >
</ImageView>
这是输出
根据您的需求进行设计
如果您想执行与这些功能不同的功能,还可以在“全部”上获得“单击事件”我将为您介绍如何使用这些功能。您可以根据自己的要求进行设计 这是密码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:padding="3dp" >
<ImageView
android:id="@+id/test_button_image"
android:layout_width="300dp"
android:layout_height="250dp"
android:layout_alignParentTop="true"
android:contentDescription="@string/app_name"
android:scaleType="fitXY"
android:src="@drawable/cardd2" >
</ImageView>
<ImageView
android:id="@+id/test_button_image1"
android:layout_width="250dp"
android:layout_height="270dp"
android:layout_alignParentTop="true"
android:contentDescription="@string/app_name"
android:scaleType="fitXY"
android:src="@drawable/cardd2" >
</ImageView>
<ImageView
android:id="@+id/test_button_image2"
android:layout_width="200dp"
android:layout_height="290dp"
android:layout_alignParentTop="true"
android:contentDescription="@string/app_name"
android:scaleType="fitXY"
android:src="@drawable/cardd2" >
</ImageView>
这是输出
根据您的需求进行设计
如果您想执行与这些功能不同的功能,还可以在“全部”上获得“单击事件”我将为您介绍如何使用这些功能。您可以根据自己的要求进行设计 这是密码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:padding="3dp" >
<ImageView
android:id="@+id/test_button_image"
android:layout_width="300dp"
android:layout_height="250dp"
android:layout_alignParentTop="true"
android:contentDescription="@string/app_name"
android:scaleType="fitXY"
android:src="@drawable/cardd2" >
</ImageView>
<ImageView
android:id="@+id/test_button_image1"
android:layout_width="250dp"
android:layout_height="270dp"
android:layout_alignParentTop="true"
android:contentDescription="@string/app_name"
android:scaleType="fitXY"
android:src="@drawable/cardd2" >
</ImageView>
<ImageView
android:id="@+id/test_button_image2"
android:layout_width="200dp"
android:layout_height="290dp"
android:layout_alignParentTop="true"
android:contentDescription="@string/app_name"
android:scaleType="fitXY"
android:src="@drawable/cardd2" >
</ImageView>
这是输出
根据您的需求进行设计
如果您想执行与这些功能不同的功能,也可以在“所有”上获得单击事件。最后,我通过图像按钮的“像素颜色”解决了我的问题。
我为我的图像按钮(卡)编写了一个OnTouch事件监听器,该监听器确定屏幕上触摸位置的颜色,然后通过将该颜色与“卡”的颜色进行比较来确定触摸了哪个“卡”。
谢谢大家。 代码如下:
findViewById(R.id.imgCard1).setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_DOWN) {
int x = (int) event.getX();
int y = (int) event.getY();
ImageView card1 = (ImageView) findViewById(R.id.imgCard1);
ImageView card2 = (ImageView) findViewById(R.id.imgCard2);
ImageView card3 = (ImageView) findViewById(R.id.imgCard3);
int color1 = Color.argb(255, 250, 250, 250);
int color2 = Color.argb(255, 209, 201, 198);
int color3 = Color.argb(255, 226, 223, 222);
if (card1 != null && isColorTouched(card1, x, y, color1)) {
// card1 is touched
} else if (card2 != null && isColorTouched(card2, x, y, color2)) {
// card2 is touched
} else if (card3 != null && isColorTouched(card3, x, y, color3)) {
// card3 is touched
}
return true; //means that event is consumed!
}
return false;
}
});
public static boolean isColorTouched(ImageView imageView, int x, int y, int color) {
final int TOLERANCE = 5;
Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
int viewH = imageView.getHeight();
int viewW = imageView.getWidth();
int h = bitmap.getHeight();
int w = bitmap.getWidth();
float hScale = 1.0f*h/viewH;
float wScale = 1.0f*w/viewW;
int scaledX = (int) (x*wScale);
int scaledY = (int) (y*hScale);
if (scaledX>=0 && scaledY>=0 && scaledX <= w && scaledY <= h) {
int pixel = bitmap.getPixel(scaledX, scaledY);
if (Math.abs(Color.red(color) - Color.red(pixel)) > TOLERANCE)
return false;
if (Math.abs(Color.green(color) - Color.green(pixel)) > TOLERANCE)
return false;
if (Math.abs(Color.blue(color) - Color.blue(pixel)) > TOLERANCE)
return false;
return true;
}
return false;
}
findViewById(R.id.imgCard1).setOnTouchListener(新视图.OnTouchListener()){
@凌驾
公共布尔onTouch(视图v,运动事件){
if(event.getAction()==MotionEvent.ACTION\u向下){
int x=(int)event.getX();
int y=(int)event.getY();
ImageView card1=(ImageView)findViewById(R.id.imgCard1);
ImageView card2=(ImageView)findViewById(R.id.imgCard2);
ImageView card3=(ImageView)findViewById(R.id.imgCard3);
int color1=Color.argb(255、250、250、250);
int color2=Color.argb(255、209、201、198);
int color3=Color.argb(255、226、223、222);
if(card1!=null&&iscolortoched(card1,x,y,color1)){
//card1被触碰了
}else if(card2!=null&&iscolortoched(card2,x,y,color2)){
//card2被触碰了
}else if(card3!=null&&iscolortoched(card3,x,y,color3)){
//card3被触碰了
}
return true;//表示事件已被消耗!
}
返回false;
}
});
公共静态布尔值是彩色的(ImageView ImageView,int x,int y,int color){
最终int公差=5;
位图位图=((BitmapDrawable)imageView.getDrawable()).getBitmap();
int-viewH=imageView.getHeight();
int-viewW=imageView.getWidth();
int h=bitmap.getHeight();
int w=bitmap.getWidth();
浮动hs比例=1.0f*h/viewH;
浮动wScale=1.0f*w/viewW;
int scaledX=(int)(x*wScale);
int scaledY=(int)(y*hScale);
如果(scaledX>=0&&scaledY>=0&&scaledX公差)
返回false;
if(Math.abs(Color.blue(Color)-Color.blue(像素))>公差)
返回false;
返回true;
}
返回false;
}
最后,我通过图像按钮的“像素颜色”解决了问题。我为我的图像按钮(卡)编写了一个OnTouch事件监听器,该监听器确定屏幕上触摸位置的颜色,然后通过将该颜色与“卡”的颜色进行比较来确定触摸了哪个“卡”。
谢谢大家。 代码如下:
findViewById(R.id.imgCard1).setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_DOWN) {
int x = (int) event.getX();
int y = (int) event.getY();
ImageView card1 = (ImageView) findViewById(R.id.imgCard1);
ImageView card2 = (ImageView) findViewById(R.id.imgCard2);
ImageView card3 = (ImageView) findViewById(R.id.imgCard3);
int color1 = Color.argb(255, 250, 250, 250);
int color2 = Color.argb(255, 209, 201, 198);
int color3 = Color.argb(255, 226, 223, 222);
if (card1 != null && isColorTouched(card1, x, y, color1)) {
// card1 is touched
} else if (card2 != null && isColorTouched(card2, x, y, color2)) {
// card2 is touched
} else if (card3 != null && isColorTouched(card3, x, y, color3)) {
// card3 is touched
}
return true; //means that event is consumed!
}
return false;
}
});
public static boolean isColorTouched(ImageView imageView, int x, int y, int color) {
final int TOLERANCE = 5;
Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
int viewH = imageView.getHeight();
int viewW = imageView.getWidth();
int h = bitmap.getHeight();
int w = bitmap.getWidth();
float hScale = 1.0f*h/viewH;
float wScale = 1.0f*w/viewW;
int scaledX = (int) (x*wScale);
int scaledY = (int) (y*hScale);
if (scaledX>=0 && scaledY>=0 && scaledX <= w && scaledY <= h) {
int pixel = bitmap.getPixel(scaledX, scaledY);
if (Math.abs(Color.red(color) - Color.red(pixel)) > TOLERANCE)
return false;
if (Math.abs(Color.green(color) - Color.green(pixel)) > TOLERANCE)
return false;
if (Math.abs(Color.blue(color) - Color.blue(pixel)) > TOLERANCE)
return false;
return true;
}
return false;
}
findViewById(R.id.imgCard1).setOnTouchListener(新视图.OnTouchListener()){
@凌驾
公共布尔onTouch(视图v,运动事件){
if(event.getAction()==MotionEvent.ACTION\u向下){
int x=(int)event.getX();
int y=(int)event.getY();
ImageView card1=(ImageView)findViewById(R.id.imgCard1);
ImageView card2=(ImageView)findViewById(R.id.imgCard2);
ImageView card3=(ImageView)findViewById(R.id.imgCard3);
int color1=Color.argb(255、250、250、250);
int color2=Color.argb(255、209、201、198);
int color3=Color.argb(255、226、223、222);
if(card1!=null&&iscolortoched(card1,x,y,color1)){
//card1被触碰了
}else if(card2!=null&&iscolortoched(card2,x,y,color2)){
//card2被触碰了
}else if(card3!=null&&iscolortoched(card3,x,y,color3)){
//card3被触碰了
}
return true;//表示事件已被消耗!
}
返回false;
}
});
公共静态布尔值是彩色的(ImageView ImageView,int x,int y,int color){
最终int公差=5;
位图位图=((BitmapDrawable)imageView.getDrawable()).getBitmap();
int viewH=imageView.getH