在api中绘制复杂的drawable,低于23

时间:2015-09-24 15:37:04

标签: java android drawable android-drawable xml-drawable

我正在尝试绘制由圆形和矩形组成的drawable。当api设置为23时它工作正常。我注意到在最新的api中添加了'item'的以下属性:高度,重力,重量。是否有可能在较旧的sdk中获得效果?

绘制对象:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:height="5dp"
        android:gravity="center_vertical"
        android:left="5dp">
        <shape android:shape="rectangle">
            <size
                android:width="50dp"
                android:height="5dp" />
            <solid android:color="#125572" />
        </shape>
    </item>
    <item
        android:width="20dp"
        android:height="20dp"
        android:gravity="center_vertical">
        <shape android:shape="oval">
            <solid android:color="#125572" />
            <size
                android:width="20dp"
                android:height="20dp" />
        </shape>
    </item>
</layer-list>

图片使用api 23:

enter image description here

图片使用api 22:

enter image description here

1 个答案:

答案 0 :(得分:-3)

我知道这个问题已经很老了,但是我解决了这个问题,也许它现在对某人有用。

对于解决方案,我创建了两个类。

一个名为MyLayerDrawable的类,它扩展了LayerDrawable。关于这个类要记住的重要一点是你可以获得每个图层的顶部和左边插图。

public class MyLayerDrawable extends LayerDrawable{


private List<Layer> layers;

//some code here

public static class Layer{
    private Drawable drawable;
    private int topInset;
    private int leftInset;
    public Layer(Drawable drawable,int topInset,int leftInset){
        this.leftInset=leftInset;
        this.drawable=drawable;
        this.topInset=topInset;
    }
    public Drawable getDrawable(){
        return drawable;
    }
    public int getTopInset(){
        return topInset;
    }
    public int getLeftInset(){return leftInset;}
}

我创建的第二个类名为MyImageViewPatch,它拥有一个ImageView。 此类的唯一公共方法是setImageMyLayerDrawable(MyLayerDrawable layerDrawable)。此方法将可绘制的图像委托给它拥有的ImageView:

public void setImageMyLayerDrawable(MyLayerDrawable layerDrawable){
    imageView.setImageDrawable(layerDrawable);

}

但在调用setImageDrawable方法之前,已将一个观察者对象添加到MyImageViewPatch构造函数中的ImageView中。在ImageView中绘制Drawable之前调用此观察器:

        imageView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
        @Override
        public boolean onPreDraw() {
            MyLayerDrawable layerDrawable=(MyLayerDrawable)imageView.getDrawable();
            int numberOfLayers=layerDrawable.getNumberOfLayers();
            Drawable drawable;
            Rect bounds;


            for(int i=0;i<numberOfLayers;i++){//for each layer
                drawable=layerDrawable.getDrawable(i);
                bounds=drawable.getBounds();
                bounds.top=layerDrawable.getLayer(i).getTopInset();
                bounds.left=layerDrawable.getLayer(i).getLeftInset();
                if(drawable.getIntrinsicHeight()<bounds.height()){
                    bounds.bottom=drawable.getIntrinsicHeight()+layerDrawable.getLayer(i).getTopInset();

                }
                if(drawable.getIntrinsicWidth()<bounds.width()){
                    bounds.right=drawable.getIntrinsicWidth()+layerDrawable.getLayer(i).getLeftInset();
                }
            }
            return true;
        }
    });

在绘制MyLayerDrawable之前可以看到,每个图层的边界都是根据左边和上边的插图进行调整的;这是使绘图以正确的尺寸绘制而不是拉伸的关键部分。

这是形状xml文件:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:height="5dp"
    android:gravity="center_vertical"
    android:left="5dp"
    android:top="8dp">
    <shape android:shape="rectangle">
        <size
            android:width="50dp"
            android:height="5dp" />
        <solid android:color="#125572" />
    </shape>
</item>

<item
android:width="20dp"
android:height="20dp">
<shape android:shape="oval">
    <solid android:color="#125572" />
    <size
        android:width="20dp"
        android:height="20dp" />
</shape>
</item>


</layer-list>

为了使矩形垂直居中,我使用了android:top =“8dp”而不是android:gravity =“center_vertical”。

以下是主要活动的代码:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ImageView iv=(ImageView)findViewById(R.id.imageView);
    MyLayerDrawable myLayerDrawable=MyLayerDrawable.Builder.build(getApplicationContext(),R.drawable.layer_d_example);
    MyImageViewPatch myImageViewPatch=new MyImageViewPatch(iv);
    myImageViewPatch.setImageMyLayerDrawable(myLayerDrawable);

}

}

我在github上有完整的代码。您可以查看:https://github.com/marcosbses/my_layout_drawable.git