Android使用Java代码设置selector或drawable,以及使用自定义控件方式使用它

本文介绍如何通过Java代码为TextView自定义背景形状及Selector状态列表,包括设置圆角、边框及不同状态下的颜色变化,并提供实用代码示例。

镇楼图~~!
这里写图片描述
TextView再给个selecotor 这种东西不要太简单,但是这种东西我不想重复去写N个Selector ! so~

  /**
     * 获取Selector
     * @param normalDraw
     * @param pressedDraw
     * @return
     */
    public static StateListDrawable getSelector(Drawable normalDraw, Drawable pressedDraw) {
        StateListDrawable stateListDrawable  = new StateListDrawable();
        stateListDrawable.addState(new int[]{ android.R.attr.state_pressed }, pressedDraw);
        stateListDrawable.addState(new int[]{ }, normalDraw);
        return stateListDrawable ;
    }

    /**
     * 设置shape(设置单独圆角)
     * @param topLeftCA
     * @param topRigthCA
     * @param buttomLeftCA
     * @param buttomRightCA
     * @param bgColor
     * @param storkeWidth
     * @param strokeColor
     * @return
     */
    public GradientDrawable getDrawable(float topLeftCA, float topRigthCA, float buttomLeftCA,
                                        float buttomRightCA, int bgColor, int storkeWidth, int strokeColor) {
        //把边框值设置成dp对应的px
        storkeWidth = dp2px(this.mContext, storkeWidth);

        float[] circleAngleArr = {topLeftCA, topLeftCA, topRigthCA, topRigthCA,
                buttomLeftCA, buttomLeftCA, buttomRightCA, buttomRightCA};
        //把圆角设置成dp对应的px
        for (int i = 0; i < circleAngleArr.length; i++){
            circleAngleArr[i] = dp2px(this.mContext, circleAngleArr[i]);
        }

        GradientDrawable gradientDrawable = new GradientDrawable();
        gradientDrawable.setCornerRadii(circleAngleArr);//圆角
        gradientDrawable.setColor(bgColor); //背景色
        gradientDrawable.setStroke(storkeWidth, strokeColor); //边框宽度,边框颜色

        return gradientDrawable;
    }

    /**
     * 设置shape(圆角)
     *
     * @param bgCircleAngle
     * @param bgColor
     * @param width
     * @param strokeColor
     * @return
     */
    public GradientDrawable getDrawable(int bgCircleAngle, int bgColor, int width, int strokeColor) {
        GradientDrawable gradientDrawable = new GradientDrawable();
        gradientDrawable.setCornerRadius(bgCircleAngle);
        gradientDrawable.setColor(bgColor);
        gradientDrawable.setStroke(width, strokeColor);
        return gradientDrawable;
    }

这里就不多说了,跟用xml设置Selector的步骤是一样的
值得注意的是:

getSelector();中

 stateListDrawable.addState(new int[]{ android.R.attr.state_pressed }, pressedDraw);

这段在设置值的时候前后要给空格,否则这个按下的状态会设置不上去,具体原因这里就不深究了。
在getDrawable有两个方法,一个是设置矩形所有角的弧度(radius),一个是单独设置每一个角的弧度(topLeftRadius,topRightRadius,bottomLeftRadius,bottomRightRadius)

 gradientDrawable.setCornerRadii(circleAngleArr);//圆角

这个方法需要传入一个有8个值的float数组,(开始没想明白,后来翻了翻源码,每一个角对应2个值,一个x一个y,好吧~高中数学都还给老师了,一个值怎么画弧线呢 - -!~)

    /**
     * <p>Specify radii for each of the 4 corners. For each corner, the array
     * contains 2 values, <code>[X_radius, Y_radius]</code>. The corners are ordered
     * top-left, top-right, bottom-right, bottom-left. This property
     * is honored only when the shape is of type {@link #RECTANGLE}.</p>
     * <p><strong>Note</strong>: changing this property will affect all instances
     * of a drawable loaded from a resource. It is recommended to invoke
     * {@link #mutate()} before changing this property.</p>
     *
     * @param radii 4 pairs of X and Y radius for each corner, specified in pixels.
     *              The length of this array must be >= 8
     *
     * @see #mutate()
     * @see #setCornerRadii(float[])
     * @see #setShape(int)
     */
    public void setCornerRadii(float[] radii) {
        mGradientState.setCornerRadii(radii);
        mPathIsDirty = true;
        invalidateSelf();
    }

代码注释写的很清楚了我就不拿我的三级英语来扯了···

使用:
如果只想添加个圆角背景那么可以这样

     GradientDrawable gradientDrawable = SelectorUtils.getInstance(this)
                .getDrawable(20, ContextCompat.getColor(this, R.color.bisque), 2, ContextCompat.getColor(this, R.color.aqua));
        editText.setBackground(gradientDrawable);

如果是Selector

       GradientDrawable normalDraw = SelectorUtils.getInstance(this)
                .getDrawable(20, ContextCompat.getColor(this, R.color.aliceblue), 2, ContextCompat.getColor(this, R.color.blueviolet));
        GradientDrawable pressDraw = SelectorUtils.getInstance(this)
               .getDrawable(20, ContextCompat.getColor(this, R.color.brown), 2, ContextCompat.getColor(this, R.color.colorPrimaryDark));

        StateListDrawable stateListDrawable = SelectorUtils.getSelector(normalDraw, pressDraw);
        textView.setBackground(stateListDrawable);

如果想单独设置每个角的弧度可以这样:

GradientDrawable normalDraw1 = SelectorUtils.getInstance(this)
                .getDrawable(10,20,0,40, ContextCompat.getColor(this, R.color.chocolate), 2, ContextCompat.getColor(this, R.color.darkcyan));
        GradientDrawable pressDraw1 = SelectorUtils.getInstance(this)
                .getDrawable(10, 10, 0, 0, ContextCompat.getColor(this, R.color.darkgoldenrod), 2, ContextCompat.getColor(this, R.color.darkorchid));

        StateListDrawable stateListDrawable1 = SelectorUtils.getSelector(normalDraw1,pressDraw1);
        textView1.setBackground(stateListDrawable1);

如果使用selector最后记得设置点击事件:

  tvText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

如果不设置点击事件,按下的drawable不会显示,原因是点击事件未分发,这里不过多解释。

是不是省去了很多selector或者drawable文件~~ 最近一直想写一个自定义控件,so~~
先来张效果图:
这里写图片描述
使用:

 <speedytextview.fmr.com.spdtvlibrary.SpdTextView
            android:id="@+id/spd5"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_marginTop="10dp"
            android:gravity="center"
            android:padding="10dp"
            android:text="方某人"
            android:textSize="16.0sp"
            app:bgClickColor="@color/yellow"
            app:bgColor="@color/aqua"
            app:buttomLeftCA="30dp"
            app:buttomRightCA="40dp"
            app:normalStorkeColor="@color/cadetblue"
            app:normalStorkeWidth="2dp"
            app:pressStorkeColor="@color/cornflowerblue"
            app:pressStorkeWidth="2dp"
            app:topLeftCA="10dp"
            app:topRightCA="20dp" />

这里需要注意下,bgColor这条属性是必须设置的,否则背景色是透明的~~原谅我的设置了默认值 - -!~ 属性这里就不多介绍了,源码都有注释(虽然是我自己写的,但是每次用的时候都要去看自己写的注释~)。

经常用一些牛逼的第三方库 免不了想给自己来一波高大上的调用 so~~
你还可以这样用:

      ((SpdTextView)findViewById(R.id.spd6))
                .setBgColor(ContextCompat.getColor(this, R.color.darkorchid))
                .setBgClickColor(ContextCompat.getColor(this, R.color.brown))
                .setNormalStorkeWidth(5)
                .setNormalStorkeColor(ContextCompat.getColor(this, R.color.burlywood))
                .setPressStorkeWidth(2)
                .setPressStorkeColor(ContextCompat.getColor(this, R.color.antiquewhite))
                .setBgCircleAngle(40)
                .build();

引入:

compile 'com.fmr:speedytextview:1.0.0'

源码

好吧 装逼结束 最后打上注释

//                            _ooOoo_  
//                           o8888888o  
//                           88" . "88  
//                           (| -_- |)  
//                            O\ = /O  
//                        ____/`---'\____  
//                      .   ' \\| |// `.  
//                       / \\||| : |||// \  
//                     / _||||| -:- |||||- \  
//                       | | \\\ - /// | |  
//                     | \_| ''\---/'' | |  
//                      \ .-\__ `-` ___/-. /  
//                   ___`. .' /--.--\ `. . __  
//                ."" '< `.___\_<|>_/___.' >'"".  
//               | | : `- \`.;`\ _ /`;.`/ - ` : | |  
//                 \ \ `-. \_ __\ /__ _/ .-` / /  
//         ======`-.____`-.___\_____/___.-`____.-'======  
//                            `=---='  
//  
//         .............................................  
//                  佛祖保佑             永无BUG 
//          佛曰:  
//                  写字楼里写字间,写字间里程序员;  
//                  程序人员写程序,又拿程序换酒钱。  
//                  酒醒只在网上坐,酒醉还来网下眠;  
//                  酒醉酒醒日复日,网上网下年复年。  
//                  但愿老死电脑间,不愿鞠躬老板前;  
//                  奔驰宝马贵者趣,公交自行程序员。  
//                  别人笑我忒疯癫,我笑自己命太贱;  
//                  不见满街漂亮妹,哪个归得程序员?  
<think>我们参考用户提供的引用资料,总结如下: 引用[1]提到需要设置TextViewdrawable的宽高,可能需要单独设置上下左右四个方向的drawable的宽高(共8个属性),者统一设置(共10个属性)。 引用[2]展示了如何通过代码设置TextViewdrawableLeft,使用的是`setCompoundDrawablesRelativeWithIntrinsicBounds`方法,但这个方法使用的是图片的固有尺寸,不能直接设置大小。 引用[3]提到了图文间距的问题,但用户的问题集中在控制drawable尺寸。 因此,用户的需求是:在Android开发中,使用Java语言为TextView设置Drawable,并且能够控制Drawable的尺寸大小。 根据Android开发经验,设置Drawable并控制其尺寸可以通过以下步骤实现: 1. 获取原始的Drawable对象。 2. 设置Drawable的边界(bounds),通过`setBounds`方法,该方法需要指定左、上、右、下四个参数,实际上就是设置drawable控件中显示的矩形区域。 3. 使用TextView的`setCompoundDrawables``setCompoundDrawablesRelative`方法将设置好边界的Drawable设置TextView的相应位置。 注意:`setCompoundDrawables`和`setCompoundDrawablesRelative`的区别在于,前者是绝对方向(左、上、右、下),后者是相对于布局方向(起始、上、结束、下)。 具体步骤: 1. 获取Drawable对象,可以通过资源ID获取:`ContextCompat.getDrawable(context, R.drawable.your_drawable)`。 2. 设置Drawable的bounds,例如:`drawable.setBounds(left, top, right, bottom)`。这里的left、top、right、bottom是相对于TextViewdrawable位置,通常我们希望设置一个固定大小,那么可以设置left=0, top=0, right=width, bottom=height,其中width和height是我们希望设置的宽高(单位是像素,所以通常需要将dp值转换为px)。 3. 调用TextView的`setCompoundDrawables``setCompoundDrawablesRelative`方法,传入四个Drawable对象,分别对应左、上、右、下(起始、上、结束、下)。对于不需要设置的位置传入null。 但是,需要注意的是,如果只设置一个方向的drawable,其他方向要保持原有的drawable不变,可以先获取原有的drawable,然后只替换需要设置drawable。如果不需要保留其他位置的drawable,可以传入null。 另外,如果只设置大小而不改变位置,那么设置bounds即可。 示例代码: ```java // 获取TextView实例 TextView textView = findViewById(R.id.textView); // 获取drawable对象(假设是left方向的drawableDrawable drawable = ContextCompat.getDrawable(this, R.drawable.ic_launcher); if (drawable != null) { // 将50dp转换为像素值 int sizeInPx = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, getResources().getDisplayMetrics()); // 设置drawable的边界,这里设置一个正方形 drawable.setBounds(0, 0, sizeInPx, sizeInPx); // 设置TextView的左侧(起始侧) // 使用setCompoundDrawablesRelative:用于支持RTL布局 textView.setCompoundDrawablesRelative(drawable, null, null, null); } ``` 另外,如果用户需要同时设置多个方向的drawable,并且分别控制大小,那么需要分别获取drawable设置bounds,然后一起设置。 注意:在设置drawable时,如果某个方向不需要改变,可以使用TextView原有的drawable。例如: ```java Drawable[] drawables = textView.getCompoundDrawablesRelative(); // 假设我们只改变左侧(起始侧)的drawable Drawable leftDrawable = ... // 获取设置bounds textView.setCompoundDrawablesRelative(leftDrawable, drawables[1], drawables[2], drawables[3]); ``` 总结: 关键点在于使用`setBounds`方法设置drawable的显示区域,然后使用`setCompoundDrawables``setCompoundDrawablesRelative`方法应用到TextView。 最后,根据指令,我们需要生成相关问题: §§相关问题§§ 1. 如何将dp单位转换为像素(px)? 2. 在Android中如何支持RTL布局? 3. TextViewdrawablePadding属性有什么作用? </think> ### 在Android使用Java控制TextViewDrawable尺寸 在Android开发中,通过Java代码控制TextViewDrawable尺寸需要以下步骤: #### 核心实现步骤 1. **获取Drawable对象** 从资源文件中加载Drawable: ```java Drawable drawable = ContextCompat.getDrawable(context, R.drawable.your_icon); ``` 2. **设置Drawable尺寸** 使用`setBounds()`方法定义尺寸(单位:像素): ```java // 将50dp转换为像素值 int sizeInPx = (int) TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_DIP, 50, getResources().getDisplayMetrics() ); drawable.setBounds(0, 0, sizeInPx, sizeInPx); // 左,上,右,下 ``` 3. **应用到TextView** 使用`setCompoundDrawables()`方法设置: ```java textView.setCompoundDrawables( drawable, // 左侧 null, // 顶部 null, // 右侧 null // 底部 ); ``` #### 完整示例代码 ```java // 获取TextView实例 TextView textView = findViewById(R.id.tv_example); // 加载drawable资源 Drawable icon = ContextCompat.getDrawable(this, R.drawable.ic_icon); // 设置尺寸为50dp x 50dp int iconSize = (int) TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_DIP, 50, getResources().getDisplayMetrics() ); icon.setBounds(0, 0, iconSize, iconSize); // 应用到TextView左侧 textView.setCompoundDrawables(icon, null, null, null); // 设置图文间距(可选) textView.setCompoundDrawablePadding(16); // 单位:像素 ``` #### 关键说明 1. **尺寸单位转换** 必须将dp值转换为像素值,使用`TypedValue.applyDimension()`方法实现[^1]。 2. **setBounds参数** `setBounds(0, 0, width, height)`中的参数定义Drawable的显示区域: - 前两个参数表示左上角坐标(通常设为0) - 后两个参数定义宽度和高度 3. **多方向设置** 若要设置多个位置的Drawable,需为每个方向单独创建并设置bounds: ```java textView.setCompoundDrawables(leftDrawable, topDrawable, rightDrawable, bottomDrawable); ``` 4. **RTL布局支持** 对于从右到左布局,使用`setCompoundDrawablesRelative()`替代[^3]: ```java textView.setCompoundDrawablesRelative(startDrawable, topDrawable, endDrawable, bottomDrawable); ``` #### 注意事项 - 直接使用`setCompoundDrawablesWithIntrinsicBounds()`无法自定义尺寸 - 调用`setBounds()`后必须重新设置DrawableTextView - 图文间距通过`setCompoundDrawablePadding()`单独控制
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值