简体   繁体   English

从Drawable创建一个没有像素透明度的形状

[英]Creating a Shape from a Drawable without pixel transparency in Android

I'm trying to create a Shape or a Rect from a Drawable but without the transparency of the Drawable. 我试图从Drawable创建一个Shape或一个Rect但没有Drawable的透明度。

As you can see here, the following button has transparency on its borders. 如您所见,以下按钮的边框具有透明度。 My issue is that I'm creating a panel Editor and I want to avoid superposition of widget when I move a widget. 我的问题是我正在创建一个面板编辑器,我想在移动一个小部件时避免重叠小部件。 To achieve this, I have used the method : Rect.intersects(Rect) 为此,我使用了以下方法: Rect.intersects(Rect)

在此输入图像描述

However, the Rect used is the shape of the entire drawable(with the transparency) and it's create too much empty space. 但是,使用的Rect是整个drawable的形状(具有透明度),并且它创建了太多的空白空间。

EDIT: more informations 编辑:更多信息

Here is my Editor, I succed to avoid superposition but the superposition is base on the Shape of the drawable (picture). 这是我的编辑,我成功避免叠加,但叠加是基于drawable(图片)的形状。 And the picture's tranparency create some empty space on my panel. 图片的透明度在我的面板上创造了一些空白空间。

在此输入图像描述

and my check collision code, where tempRect is the current Moving widget 和我的检查碰撞代码,其中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" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM