在Android中创建一个没有像素透明度的Drawable形状

3

我想从一个Drawable创建一个形状或矩形,但不带有Drawable的透明度。

正如您在此处所见,以下按钮在其边框上具有透明度。我的问题是,我正在创建一个面板编辑器,并且我想在移动小部件时避免小部件的叠加。为了实现这一点,我使用了以下方法: Rect.intersects(Rect)

enter image description here

然而,使用的矩形是整个drawable的形状(包括透明度),这样会创建太多的空白空间。

编辑: 更多信息

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

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;
}
1个回答

0
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" />

我可能没有表达清楚,我需要从已保存为Drawable的现有图片创建一个Shape或Rect,但是不带图片的透明度。 - thibault larour

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接