位置: 编程技术 - 正文

[置顶] Android GradientDrawable高级应用 以后完全用不上美工了([置顶]bilinovel)

编辑:rootadmin

推荐整理分享[置顶] Android GradientDrawable高级应用 以后完全用不上美工了([置顶]bilinovel),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:[置顶]JM259194,[置顶]公主大人接下来是拷问时间31,[置顶]电影名字《收件人不详》,[置顶]游戏名:chivalry2,[置顶]游戏名:chivalry2,[置顶]游戏名:chivalry2,[置顶]电影名字《收件人不详》,[置顶]bilinovel,内容如对您有帮助,希望把文章链接给更多的朋友!

先看截图 1图为自定义的Textview 2、3图为点击效果

具体实现如下:

1. 定义自定义控件属性

<declare-styleable name="ShapeTextview"> <attr name="touchSolidColor" format="color" /> <attr name="solidColor" format="color" /> <attr name="cornesRadius" format="dimension" /> <attr name="topLeftRadius" format="dimension" /> <attr name="topRightRadius" format="dimension" /> <attr name="bottomLeftRadius" format="dimension" /> <attr name="bottomRightRadius" format="dimension" /> <attr name="stroke_Width" format="dimension" /> <attr name="stroke_Color" format="color" /> <attr name="strokeDashWidth" format="dimension" /> <attr name="strokeDashGap" format="dimension" /> <attr name="gradientStartColor" format="color" /> <attr name="gradientEndColor" format="color" /> <attr name="gradientCenterColor" format="color" /> <attr name="gradientUseLevel" format="boolean" /> <attr name="gradientAngle" format="dimension" /> <attr name="gradientOrientation"> <enum name="BL_TR" value="0" /> <enum name="BOTTOM_TOP" value="1" /> <enum name="BR_TL" value="2" /> <enum name="LEFT_RIGHT" value="3" /> <enum name="RIGHT_LEFT" value="4" /> <enum name="TL_BR" value="5" /> <enum name="TOP_BOTTOM" value="6" /> <enum name="TR_BL" value="7" /> </attr> <attr name="shapeType"> <enum name="LINEAR_GRADIENT" value="0" /> <enum name="OVAL" value="1" /> <enum name="LINE" value="2" /> <enum name="RING" value="3" /> </attr> <attr name="gradientType"> <enum name="linear" value="0" /> <enum name="radial" value="1" /> <enum name="sweep" value="2" /> </attr> <attr name="gradientRadius" format="dimension" /> </declare-styleable>

2. 控件代码

package com.klower.component;

import android.content.Context;import android.content.res.TypedArray;import android.graphics.Color;import android.graphics.drawable.GradientDrawable;import android.util.AttributeSet;import android.view.MotionEvent;import android.widget.TextView;

import com.klower.R;

public class ShapeTextView extends TextView {

int solidColor, stroke_Color, gradientStartColor, gradientEndColor, gradientCenterColor, touchColor;

int cornesRadius, topLeftRadius, topRightRadius, bottomLeftRadius, bottomRightRadius, stroke_Width, strokeDashWidth, strokeDashGap, gradientAngle, gradientRadius, gradientType, gradientOrientation, shapeType; boolean gradientUseLevel;

GradientDrawable gradientDrawable;

[置顶]
        Android GradientDrawable高级应用 以后完全用不上美工了([置顶]bilinovel)

public ShapeTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); }

public ShapeTextView(Context context, AttributeSet attrs) { super(context, attrs); initData(context, attrs); }

public ShapeTextView(Context context) { super(context); }

private void initData(Context context, AttributeSet attrs) { TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ShapeTextview); solidColor = a.getColor(R.styleable.ShapeTextview_solidColor, Color.TRANSPARENT); stroke_Color = a.getColor(R.styleable.ShapeTextview_stroke_Color, Color.TRANSPARENT); gradientStartColor = a .getColor(R.styleable.ShapeTextview_gradientStartColor, Color.TRANSPARENT); gradientEndColor = a.getColor( R.styleable.ShapeTextview_gradientEndColor, Color.TRANSPARENT); gradientCenterColor = a.getColor( R.styleable.ShapeTextview_gradientCenterColor, Color.TRANSPARENT); touchColor = a.getColor(R.styleable.ShapeTextview_touchSolidColor, Color.TRANSPARENT);

cornesRadius = (int) a.getDimension( R.styleable.ShapeTextview_cornesRadius, 0); topLeftRadius = (int) a.getDimension( R.styleable.ShapeTextview_topLeftRadius, 0); topRightRadius = (int) a.getDimension( R.styleable.ShapeTextview_topRightRadius, 0); bottomLeftRadius = (int) a.getDimension( R.styleable.ShapeTextview_bottomLeftRadius, 0); bottomRightRadius = (int) a.getDimension( R.styleable.ShapeTextview_bottomRightRadius, 0); stroke_Width = (int) a.getDimension( R.styleable.ShapeTextview_stroke_Width, 0); strokeDashWidth = (int) a.getDimension( R.styleable.ShapeTextview_strokeDashWidth, 0); strokeDashGap = (int) a.getDimension( R.styleable.ShapeTextview_strokeDashGap, 0); gradientAngle = (int) a.getDimension( R.styleable.ShapeTextview_gradientAngle, 0); gradientRadius = (int) a.getDimension( R.styleable.ShapeTextview_gradientRadius, 0); gradientUseLevel = a.getBoolean( R.styleable.ShapeTextview_gradientUseLevel, false); gradientType = a.getInt(R.styleable.ShapeTextview_gradientType, -1); gradientOrientation = a.getInt( R.styleable.ShapeTextview_gradientOrientation, -1); shapeType = a.getInt( R.styleable.ShapeTextview_shapeType, -1); gradientDrawable = new GradientDrawable(); gradientDrawable.setStroke(stroke_Width, stroke_Color, strokeDashWidth, strokeDashGap); // 如果设定的有Orientation 就默认为是渐变色的Button,否则就是纯色的Button if (gradientOrientation != -1) { gradientDrawable .setOrientation(getOrientation(gradientOrientation)); gradientDrawable.setColors(new int[] { gradientStartColor, gradientCenterColor, gradientEndColor }); } else { gradientDrawable.setColor(solidColor); } if(shapeType != -1){ gradientDrawable.setShape(shapeType); } //是否为圆形 if(shapeType != GradientDrawable.OVAL){ // 如果设定的有Corner Radius就认为是4个角一样的Button, 否则就是4个不一样的角 Button if (cornesRadius != 0) { gradientDrawable.setCornerRadius(cornesRadius); } else { //1、2两个参数表示左上角,3、4表示右上角,5、6表示右下角,7、8表示左下角 gradientDrawable.setCornerRadii(new float[] { topLeftRadius, topLeftRadius, topRightRadius, topRightRadius, bottomRightRadius, bottomRightRadius, bottomLeftRadius, bottomLeftRadius }); } } if (gradientUseLevel) gradientDrawable.setUseLevel(gradientUseLevel); if (gradientType != -1) gradientDrawable.setGradientType(gradientType); gradientDrawable.setGradientRadius(gradientRadius); setBackground(gradientDrawable);

}

@Override public boolean onTouchEvent(MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { if (touchColor != Color.TRANSPARENT) { gradientDrawable.setColor(touchColor); setBackground(gradientDrawable); postInvalidate(); } } else if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL) { if (touchColor != Color.TRANSPARENT) { gradientDrawable.setColor(solidColor); setBackground(gradientDrawable); } } return super.onTouchEvent(event); }

private GradientDrawable.Orientation getOrientation(int gradientOrientation) { GradientDrawable.Orientation orientation = null; switch (gradientOrientation) { case 0: orientation = GradientDrawable.Orientation.BL_TR; break; case 1: orientation = GradientDrawable.Orientation.BOTTOM_TOP; break; case 2: orientation = GradientDrawable.Orientation.BR_TL; break; case 3: orientation = GradientDrawable.Orientation.LEFT_RIGHT; break; case 4: orientation = GradientDrawable.Orientation.RIGHT_LEFT; break; case 5: orientation = GradientDrawable.Orientation.TL_BR; break; case 6: orientation = GradientDrawable.Orientation.TOP_BOTTOM; break; case 7: orientation = GradientDrawable.Orientation.TR_BL; break; } return orientation; }

}

3. xml 加上这句xmlns:flatui="

<!-- flatui:strokeDashGap="5dp" flatui:strokeDashWidth="5dp" flatui:gradientOrientation = "BOTTOM_TOP" -->

<com.klower.component.ShapeTextView android:id="@&#;id/shapetextview" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="dp" android:gravity="center" android:padding="5dp" android:text="ShapeTextview" android:textSize="sp" flatui:bottomLeftRadius="dp" flatui:bottomRightRadius="0dp" flatui:gradientCenterColor="#" flatui:gradientEndColor="#DE0E" flatui:gradientStartColor="#DB1E" flatui:solidColor="#DC" flatui:stroke_Color="#DB1E" flatui:stroke_Width="2dp" flatui:topLeftRadius="0dp" flatui:topRightRadius="dp" flatui:touchSolidColor="#F5B2B9" /> <com.klower.component.ShapeTextView flatui:strokeDashGap="5dp" flatui:strokeDashWidth="5dp" android:id="@&#;id/shapetextview1" android:layout_width="dp" android:layout_height="dp" android:layout_margin="dp" android:gravity="center" android:padding="5dp" android:text="ORAL" android:textSize="sp" flatui:gradientCenterColor="#" flatui:gradientEndColor="#DE0E" flatui:gradientStartColor="#DB1E" flatui:solidColor="#DC" flatui:stroke_Color="#DB1E" flatui:stroke_Width="2dp" flatui:shapeType = "OVAL" flatui:touchSolidColor="#F5B2B9" />

具体效果可以自己可以看源码 然后调试属性 只要你想要做出的效果基本都可以实现

有问题可以留言

Services 翻译第二集 原文地址:

Android 获取手机屏幕的宽度和高度 WindowManagerwm=(WindowManager)getContext().getSystemService(Context.WINDOW_SERVICE);intwidth=wm.getDefaultDisplay().getWidth();intheight=wm.getDefaultDisplay().getHeight();2、WindowManagerwm=this.ge

ScrollView用法(一) 理论部分1、ScrollView和HorizontalScrollView是为控件或者布局添加滚动条2、上述两个控件只能有一个孩子,但是它并不是传统意义上的容器3、上述两个控件

标签: [置顶]bilinovel

本文链接地址:https://www.jiuchutong.com/biancheng/386101.html 转载请保留说明!

上一篇:androidday3

下一篇:Services 翻译第二集(services的翻译)

  • 会计运费怎么算的
  • 社保局发放的稳岗补贴怎么入账
  • 小规模在税务局开的专票已交税,如何在税务平台申报
  • 餐饮业增值税是多少
  • 出口货物不能退税的原因
  • 网线的税收编码是多少
  • 装卸收入的增值税税率是多少
  • 怎么在网上申请变更姓名
  • 企业所得税逾期未申报怎么补申报
  • 单位统一为员工办的银行卡属于对公账户吗
  • 出差补贴没有发票
  • 科技型中小企业认定需要什么条件
  • 货物出口被海关查
  • 购入资产当月开始计提折旧
  • 微信公众号认证年审怎么弄
  • 进项税和销项税的分录
  • win10用户头像错误 设置用户头像失败,请再试一次
  • 出口退税率和进项一致吗
  • 税前扣除税后扣除
  • 临时股东大会的召开情形
  • linux如何放大字体
  • 企业年金基金收益
  • 一次摊销法优缺点
  • 同一控制下的控股合并与非同一控制下的控股合并
  • 超市进货产品
  • vue ui怎么用
  • 融资租入固定资产是什么意思
  • 申报表30栏一直不填写会怎么样
  • Yii2超好用的日期和时间组件(值得收藏)
  • 房地产公司收房款怎么分录
  • 漏记收入 罚款
  • 购进来的样品怎么账务处理
  • php怎么修改当前用户的密码
  • sqlsever日志在哪儿
  • 网吧相关规定
  • 年报资金数额是什么意思
  • SQL Server中的XML数据进行insert、update、delete
  • row number函数的使用场景
  • 修改mysql配置
  • 增值税专用发票几个点
  • 应收账款的贷方发生额表示什么
  • 公司垫付生育津贴后,社保局未报销给公司
  • 资产处置损益属于什么会计要素
  • 生产企业购进的生产原材料用来装修公司会计处理
  • 一般纳税人每月开票限额是多少
  • 公司注册资本减资流程
  • 完工产品制造成本表
  • 收到美元收入如何入账
  • 企业退休职工取暖费
  • 公对公房产过户
  • 资产负债表上应交税费是什么意思
  • 持有至到期投资改名为
  • 招标付款条件及比例
  • 应付职工薪酬属不属于流动负债
  • 与其他公司合伙协议书
  • sql server 3417错误
  • mysql死锁的情况
  • 系统32位和64位的区别 Windows系统32位和64位的区别在哪里
  • windows7 记事本
  • 在linux2.4.0版本中
  • linux计划任务怎么写
  • 微软报错是什么意思
  • NGUI字体图集导致Label文字破碎的BUG以及解决方案
  • js怎么用
  • django分层
  • 也论主板的扩展性
  • android插件化原理面试
  • javascript 二维数组搜索
  • unity的spine动画切换
  • android错误报告
  • 每天一篇日记100字
  • JavaScript电子时钟倒计时
  • javascript如何学
  • python对文件操作采用的统一
  • 税务局24小时人工服务电话
  • 保险公司优惠的款项什么时候退款
  • 税率的计算器在线计算
  • 新疆捡陨石地方叫什么
  • 买车没有按时交车怎么办
  • 个人所得税的工资比实际的多
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设