Android下基于XML的Graphics

以前作图,一般有两种方式,首先是UI把图形设计好,我们直接贴,对于那些简单的图形,如矩形、扇形这样的图形,一般的系统的API会提供这样的接口,但是在Android下,有第三种画图方式,介于二者之间,结合二者的长处,如下的代码:

    <item android:id="@android:id/secondaryProgress">
        <clip>
          
            <shape>
                <corners android:radius="5dip" />
                <gradient
                        android:startColor="#0055ff88"
                        android:centerColor="#0055ff00"
                        android:centerY="0.75"
                        android:endColor="#00320077"
                        android:angle="270"
                />
            </shape>
        </clip>
    </item>

 

这是一个Progress的style里面的代码,描述的是进度条的为达到的图形,原本以为这是一个图片,后来仔细的跟踪代码,发现居然是xml,像这种shape corners gradient等等这还是第一次碰到。shape 表示是一个图形,corners表示是有半径为5像素的圆角,然后,gradient表示一个渐变。这样作图简单明了,并且可以做出要求很好的图形,并且节省资源。

如下:

<item>
    <shape xmlns:android="http://schemas.android.com/apk/res/android " android:shape ="rectangle">
   <stroke android:width="1dip" android:color="#FBBB" />
     <solid android:color="#6000"/>
      <layout_margin android:layout_margin="1dp"/>
      <corners
         android:bottomRightRadius="18dip"
         android:bottomLeftRadius="6dip"
         android:topLeftRadius="6dip"
         android:topRightRadius="6dip"/>

     </shape >
</item>

 

如果我们想用图形来代替可以试一下下面的代码

<clip android:drawable="@drawable/myProgressDrawable" />

 

如果想填充空白就使用

<shape xmlns:android="http://schemas.android.com/apk/res/android "
android:shape ="rectangle">
    <gradient android:startColor="#FF000044"
              android:endColor="#FF0000FF"
              android:angle="180"
     />
    <padding android:left="7dp" android:top="7dp"
            android:right="7dp" android:bottom="7dp" />
    <corners android:radius="8dp" />
</shape >

 

 

Hi,

I am trying to customize background drawable for Gallery widget in android.
So I took the Gallery1.java in APIDemo and try changing the background
drawable of Gallery.

In Gallery1.java, I add:
        // Reference the Gallery view
        Gallery g = (Gallery) findViewById(R.id.gallery);

        g.setFocusable(true);
        g.setFocusableInTouchMode(true);
        g.setBackgroundResource(R.drawable.gallery_background_1);

In the gallery_background_1.xml, I have
<selector xmlns:android="http://schemas.android.com/apk/res/android ">
    <item android:state_pressed="true" android:drawable="@drawable/blue_box" />
    <item android:drawable="@drawable/red_box" />
</selector>

When I run in the emulator, I do see the red color as the background
of the gallery.
But I never see it changes to blue as I pressed the gallery.

Can you please tell me what am I missing?

Thank you for your help.

Here is the red_box and blue_box drawable:
blue_box.xml:

 <shape xmlns:android="http://schemas.android.com/apk/res/android ">
    <solid android:color="#1e0000ff"/>
    <padding android:left="1dp" android:top="1dp"
            android:right="1dp" android:bottom="1dp" />
</shape >

red_box.xml:

<shape xmlns:android="http://schemas.android.com/apk/res/android ">
    <solid android:color="#1eff0000"/>
    <padding android:left="1dp" android:top="1dp"
            android:right="1dp" android:bottom="1dp" />
</shape >

你可能感兴趣的:(android,xml,api,layout,reference,图形)