StateListDrawable资源和ShapeDrawable资源的使用

随时随地阅读更多技术实战干货,获取项目源码、学习资料,请关注源代码社区公众号(ydmsq666)

StateListDrawable用于组织多个Drawable对象,StateListDrawable对象所显示的Drawable对象会随目标组件状态的改变而自动切换。StateListDrawable对象的XML文件的根元素为<selector>,该元素可以包含多个<item.../>元素,其所支持的状态有:

ShapeDrawable用于定义一个基本的几何图形(如矩形、圆形、线条等),定义ShapeDrawable的XML文件的根元素是<shape.../>元素,该元素可以指定如下属性:

android:shape=["rectangle"|"oval"|"line"|"ring"]

下面这个实例主要实现:点击按钮时,按钮背景切换。以及自制按钮边框和颜色渐变。

Activity:

package com.lovo;

import android.app.Activity;
import android.os.Bundle;

import com.lovo.R;

public class TestDrawableActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.test_drawable_layout);
	}
}

布局XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/drawable_btn_press"
        android:text="登录" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/drawable_edittext_border" />

</LinearLayout>

drawable中的实现颜色渐变的XML:drawable_edittext_border.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <!-- 背景填充色 -->
    <solid android:color="#ff0" />
    <!-- 内边距 -->
    <padding
        android:bottom="10dp"
        android:left="10dp"
        android:right="10dp"
        android:top="10dp" />
    <!--
		边框设定
		width:边框大小
		color:边框颜色


    -->
    <stroke
        android:width="2dp"
        android:color="#00f" />
    <!--
		设置圆角矩形
		radius:四个圆角的大小。也可以单独设置


    -->
    <corners android:radius="10dp" />
    <!--
    	设置背景渐变色
	    startColor:开始颜色
	    endColor:结束颜色
	    centerColor:中间颜色

    -->
    <gradient
        android:endColor="#80ff00ff"
        android:startColor="#ffff0000" />

</shape>


drawable中的实现按钮点击背景变化的XML:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/btn_login_sel" android:state_pressed="true"></item>
    <item android:drawable="@drawable/btn_login_nor" android:state_pressed="false"></item>

</selector>


附上图片效果:

点击时:

松开时:



 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
ShapeDrawableAndroid的一个Drawable类型,它可以让开发者自定义绘制出各种形状的图形。下面是ShapeDrawable的用法: 1.创建一个ShapeDrawable对象 可以通过如下方式创建一个ShapeDrawable对象: ``` ShapeDrawable shapeDrawable = new ShapeDrawable(); ``` 2.设置ShapeDrawable的形状 在创建ShapeDrawable对象后,需要设置它的形状。ShapeDrawable支持以下几种形状: 矩形(RectangleShape): ``` shapeDrawable.setShape(new RectShape()); ``` 圆形(OvalShape): ``` shapeDrawable.setShape(new OvalShape()); ``` 线条(LineShape): ``` shapeDrawable.setShape(new LineShape()); ``` 圆角矩形(RoundRectShape): ``` float[] radii = {10, 10, 10, 10, 0, 0, 0, 0}; // 每个角的半径 RectF rectF = new RectF(0, 0, 100, 100); // 矩形的位置和大小 shapeDrawable.setShape(new RoundRectShape(radii, rectF, radii)); ``` 3.设置ShapeDrawable颜色 设置ShapeDrawable颜色可以使用setColor()方法: ``` shapeDrawable.getPaint().setColor(Color.RED); ``` 4.设置ShapeDrawable边框 设置ShapeDrawable边框可以使用setStroke()方法: ``` shapeDrawable.getPaint().setStrokeWidth(5); shapeDrawable.getPaint().setStyle(Paint.Style.STROKE); shapeDrawable.getPaint().setColor(Color.BLACK); ``` 5.使用ShapeDrawable 最后,可以将ShapeDrawable对象设置为View的背景或者ImageView的src属性,或者直接在自定义绘制的时候使用它。例如,在Activity的onCreate()方法设置一个矩形ShapeDrawable的背景: ``` ShapeDrawable shapeDrawable = new ShapeDrawable(); shapeDrawable.setShape(new RectShape()); shapeDrawable.getPaint().setColor(Color.RED); View view = findViewById(R.id.view); view.setBackground(shapeDrawable); ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

u010142437

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值