android 图形图像,Android图形图像Drawable的使用(二)

在本文中,程序统一使用的原图为image.jpg,如图 1。

4c94c7e743d07a8894185501382672d2.png

图 1

(1) BitmapDrawable

首先是MainActivity的布局文件activity_main.xml。

本文引用地址:http://emb.hqyj.com/Column/7551.html

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="horizontal"

tools:context="com.example.drawabletest.MainActivity" >

android:id="@+id/text1"

android:layout_width="100dp"

android:layout_height="100dp"

android:layout_margin="2dp"

android:background="@drawable/bitmap_drawable_repeat" />

android:id="@+id/text2"

android:layout_width="100dp"

android:layout_height="100dp"

android:layout_margin="2dp"

android:background="@drawable/bitmap_drawable_mirror" />

android:id="@+id/text3"

android:layout_width="100dp"

android:layout_height="100dp"

android:layout_margin="2dp"

android:background="@drawable/bitmap_drawable_clamp" />

上面三个TextView分别使用了不同的BitmapDrawable的tileMode,下面分别贴出这三个Drawable的xml文件。

bitmap_drawable_repeat.xml:

android:src="@drawable/image"

android:tileMode="repeat" />

bitmap_drawable_mirror.xml:

android:src="@drawable/image"

android:tileMode="mirror" />

bitmap_drawable_clamp.xml:

android:src="@drawable/image"

android:tileMode="clamp" />

显示效果如图 2。

e55f0f7fafc3c6156560808942ea2ae4.png

图 2

(2) ShapeDrawable

首先是MainActivity的布局文件activity_main.xml。

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="horizontal"

tools:context="com.example.drawabletest.MainActivity" >

android:id="@+id/text1"

android:layout_width="100dp"

android:layout_height="100dp"

android:layout_margin="2dp"

android:background="@drawable/shape_drawable_gradient_linear" />

android:id="@+id/text2"

android:layout_width="100dp"

android:layout_height="100dp"

android:layout_margin="2dp"

android:background="@drawable/shape_drawable_gradient_radius" />

android:id="@+id/text3"

android:layout_width="100dp"

android:layout_height="100dp"

android:layout_margin="2dp"

android:background="@drawable/shape_drawable_gradient_sweep" />

上面三个TextView分别使用了不同的ShapeDrawable的type,下面分别贴出这三个Drawable的xml文件。

shape_drawable_gradient_linear.xml:

android:shape="rectangle" >

android:angle="45"

android:centerColor="#00ff00"

android:centerX="0.5"

android:centerY="0.5"

android:endColor="#0000ff"

android:startColor="#ff0000"

android:type="linear" />

shape_drawable_gradient_radius.xml:

android:shape="rectangle" >

android:centerColor="#00ff00"

android:endColor="#0000ff"

android:gradientRadius="50"

android:startColor="#ff0000"

android:type="radial" />

shape_drawable_gradient_sweep.xml:

android:shape="rectangle" >

android:centerColor="#00ff00"

android:endColor="#0000ff"

android:gradientRadius="50"

android:startColor="#ff0000"

android:type="radial" />

显示效果如图 3。

2a99af636e697ccd26f7b94d137013ca.png

图 3

(3) LayerDrawable

直接贴出layer_drawable.xml代码,作为TextView的background。

<>

android:bottom="10dp"

android:left="10dp"

android:right="10dp">

显示效果如图 4。

69fc2463c26102d174a62d4319bd7707.png

图 4

(4) StateListDrawable

直接贴出statelist_drawable.xml代码,作为TextView的background。

android:drawable="@drawable/button_pressed"

android:state_pressed="true"/>

android:drawable="@drawable/button_focused"

android:state_focused="true"/>

(5) LevelListDrawable

直接贴出levellist_drawable.xml代码,作为TextView的background。

android:drawable="@drawable/low_level"

android:maxLevel="1000"

android:minLevel="0"/>

android:drawable="@drawable/high_level"

android:maxLevel="10000"

android:minLevel="1001"/>

(6) InsetDrawable

直接贴出inset_drawable.xml代码,作为TextView的background。

android:insetBottom="15dp"

android:insetLeft="15dp"

android:insetRight="15dp"

android:insetTop="15dp" >

(7) ClipDrawable

直接贴出clip_drawable.xml代码,作为ImageView的background。

android:clipOrientation="vertical"

android:drawable="@drawable/image"

android:gravity="bottom" />

ImageView的布局为:

android:id="@+id/img1"

android:layout_width="100dp"

android:layout_height="100dp"

android:gravity="center"

android:src="@drawable/clip_drawable" />

MainActivity.java代码中:

ImageView mImageView = (ImageView) findViewById(R.id.img1);

ClipDrawable clipDrawable = (ClipDrawable)mImageView.getDrawable();

clipDrawable.setLevel(5000);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值