gpt4 book ai didi

java - 在 Android 中从没有像素透明度的 Drawable 创建形状

转载 作者:太空狗 更新时间:2023-10-29 14:48:13 27 4
gpt4 key购买 nike

我正在尝试从 Drawable 创建 Shape 或 Rect,但没有 Drawable 的透明度。

正如您在此处看到的,以下按钮的边框是透明的。我的问题是我正在创建一个面板编辑器,我想在移动小部件时避免小部件叠加。为此,我使用了以下方法:Rect.intersects(Rect)

enter image description here

但是,使用的 Rect 是整个可绘制对象的形状(具有透明度),它会产生太多的空白空间。

编辑:更多信息

这是我的编辑器,我成功地避免了叠加,但叠加是基于可绘制对象(图片)的形状。图片的透明度在我的面板上创建了一些空白区域。

enter image description here

和我的检查碰撞代码,其中 tempRect 是当前的移动小部件

public boolean checkCollision(Rect tempRect,ArrayList<IHMObject> listWidget) {
float dimen = activity.getResources().getDimension(R.dimen.abc_action_bar_default_height)+ 38;
for(IHMObject widget :listWidget ){
int[]location = new int[2];
View v =(View)widget.getView();
v.getLocationInWindow(location);
//vérifier la postion du widget en fonctions des autres widgets de l'IHM
Rect staticRect = new Rect(location[0],location[1]-(int)dimen,location[0]+v.getWidth(),location[1]+v.getHeight()-(int)dimen);
if (this.id!=widget.id){
if (staticRect.intersect(tempRect)) {
//il y a une collision entre les deux rectangles
return true;
}
}

}
return false;
}

最佳答案

Create a round_shape.xml file in drawable folder like below


<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
<!-- you can use any color you want I used here gray color-->
<solid android:color="#ABABAB"/>
<corners android:radius="10dp"/>
</shape>

Set this Draweble like below

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textColor="#ffffff"
android:src="@drawable/round_shape" // here is your round_shape
android:background="#000000"
android:text="Buttons" />

关于java - 在 Android 中从没有像素透明度的 Drawable 创建形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37653817/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com