代码 实现按钮 seletor shape 选择器等
来源:互联网 发布:张兆艺淘宝店 编辑:程序博客网 时间:2024/05/19 21:01
package com.fangwf.shapeviewcode.util;import android.content.Context;import android.graphics.Color;import android.graphics.drawable.Drawable;import android.graphics.drawable.GradientDrawable;import android.graphics.drawable.StateListDrawable;import android.os.Build;import android.support.v4.content.ContextCompat;import android.view.View;import com.fangwf.shapeviewcode.widget.OvalDrawable;public class ShapeBackgroundUtil { /** * 设置自定义颜色的drawable背景 * * @param context * @param view * @param drawableId drawable-shape-背景 * @param color 自定颜色 */ public static void setCustomColorShape(Context context, View view, int drawableId, String color) { setCustomColorShape(context, view, drawableId, Color.parseColor(color)); } public static void setCustomColorShape(Context context, View view, int drawableId, int color) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { view.setBackground(ContextCompat.getDrawable(context, drawableId)); } else { view.setBackgroundDrawable(ContextCompat.getDrawable(context, drawableId)); } GradientDrawable myGrad = (GradientDrawable) view.getBackground(); myGrad.setColor(color); } /** * 设置自定义颜色的全圆角长形按钮背景 * * @param view * @param color */ public static void setCustomColorOvalBackground(View view, String color) { setCustomColorOvalBackground(view, Color.parseColor(color)); } public static void setCustomColorOvalBackground(View view, int color) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { view.setBackground(new OvalDrawable(color)); } else { view.setBackgroundDrawable(new OvalDrawable(color)); } } public static void setMultiColorOvalBackground(View view, String defaultColor, String activeColor) { setMultiColorOvalBackground(view, Color.parseColor(defaultColor), Color.parseColor(activeColor)); } public static void setMultiColorOvalBackground(View view, int defaultColor, int activeColor) { StateListDrawable drawable = new StateListDrawable(); Drawable drawable1 = new OvalDrawable(defaultColor); Drawable drawable2 = new OvalDrawable(activeColor); drawable.addState(new int[]{-android.R.attr.state_enabled}, drawable2); drawable.addState(new int[]{android.R.attr.state_selected}, drawable2); drawable.addState(new int[]{android.R.attr.state_active}, drawable2); drawable.addState(new int[]{android.R.attr.state_checked}, drawable2); drawable.addState(new int[]{android.R.attr.state_pressed}, drawable2); drawable.addState(new int[]{android.R.attr.state_focused}, drawable2); drawable.addState(new int[]{}, drawable1); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { view.setBackground(drawable); } else { view.setBackgroundDrawable(drawable); } } public static void setMultiColorBackground(Context context, View view, int drawableId, String defaultColor, String activeColor) { setMultiColorBackground(context, view, drawableId, Color.parseColor(defaultColor), Color.parseColor(activeColor)); } public static void setMultiColorBackground(Context context, View view, int drawableId, int defaultColor, int activeColor) { StateListDrawable drawable = new StateListDrawable(); GradientDrawable drawable1 = (GradientDrawable) ContextCompat.getDrawable(context, drawableId); GradientDrawable drawable2 = (GradientDrawable) ContextCompat.getDrawable(context, drawableId); drawable1.setColor(defaultColor); drawable2.setColor(activeColor);// drawable.addState(new int[]{android.R.attr.state_enabled}, drawable2); drawable.addState(new int[]{android.R.attr.state_active}, drawable2); drawable.addState(new int[]{android.R.attr.state_checked}, drawable2); drawable.addState(new int[]{android.R.attr.state_pressed}, drawable2); drawable.addState(new int[]{android.R.attr.state_selected}, drawable2); drawable.addState(new int[]{android.R.attr.state_focused}, drawable2); drawable.addState(new int[]{}, drawable1); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { view.setBackground(drawable); } else { view.setBackgroundDrawable(drawable); } }}
package com.fangwf.shapeviewcode.activity;import android.app.Activity;import android.os.Bundle;import android.view.View;import com.fangwf.shapeviewcode.R;import com.fangwf.shapeviewcode.util.ShapeBackgroundUtil;public class MainActivity extends Activity implements View.OnClickListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ShapeBackgroundUtil.setMultiColorBackground(this, findViewById(R.id.button), R.drawable.shape_radius, "#e26119", "#df3816"); ShapeBackgroundUtil.setMultiColorOvalBackground(findViewById(R.id.button2), "#ea517d", "#df3816"); ShapeBackgroundUtil.setCustomColorOvalBackground(findViewById(R.id.button3), "#0d3fb3"); ShapeBackgroundUtil.setCustomColorShape(this,findViewById(R.id.button4),R.drawable.shape_radius,"#e26119"); findViewById(R.id.button).setOnClickListener(this); findViewById(R.id.button2).setOnClickListener(this); } @Override public void onClick(View v) { }}
R.drawable.shape_radius
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" > <corners android:topLeftRadius="5dp" android:topRightRadius="5dp" android:bottomLeftRadius="5dp" android:bottomRightRadius="5dp"/> <solid android:color="#00000000"/></shape>
package com.fangwf.shapeviewcode.widget;import android.graphics.Canvas;import android.graphics.ColorFilter;import android.graphics.Paint;import android.graphics.Rect;import android.graphics.RectF;import android.graphics.drawable.Drawable;public class OvalDrawable extends Drawable { public OvalDrawable() { this(0); } public OvalDrawable(int color) { mPaint.setColor(color); mPaint.setStyle(Paint.Style.FILL); mPaint.setAntiAlias(true); } private boolean mRebuildShader = true; private final Paint mPaint = new Paint(); private RectF leftRect, rightRect, cententRect; @Override public void draw(Canvas canvas) { if (mRebuildShader) { Rect bounds = getBounds(); int r = bounds.bottom - bounds.top; leftRect = new RectF(bounds.left, bounds.top, bounds.left + r, bounds.top + r); rightRect = new RectF(bounds.right - r, bounds.top, bounds.right, bounds.top + r); cententRect = new RectF(bounds.left + r / 2, bounds.top, bounds.right - r / 2, bounds.bottom); } canvas.drawArc(leftRect, 90, 180, true, mPaint); canvas.drawRect(cententRect, mPaint); canvas.drawArc(rightRect, -90, 180, true, mPaint); } @Override protected void onBoundsChange(Rect bounds) { super.onBoundsChange(bounds); mRebuildShader = true; } public void setColor(int color) { mPaint.setColor(color); } public int getColor() { return mPaint.getColor(); } @Override public void setAlpha(int alpha) { mPaint.setAlpha(alpha); } @Override public int getAlpha() { return mPaint.getAlpha(); } @Override public void setColorFilter(ColorFilter cf) { } @Override public int getOpacity() { return 0; }}
0 0
- 代码 实现按钮 seletor shape 选择器等
- shape与seletor状态选择器
- Android shape 和Seletor
- Android按钮shape形状资源代码实现
- Button 提交按钮+颜色变化选择器 shape 实现
- Android Shape实现选择器样式
- Android 代码里动态设置TextView/Button等的文字颜色Seletor
- Seletor()
- Android中XML绘图:Bitmap/Shape/Seletor和Layer
- Android利用shape实现圆形按钮
- java代码编写shape文件和状态选择器
- 自绘按钮实现颜色选择器
- 自绘按钮实现颜色选择器
- 自绘按钮实现颜色选择器
- 自绘按钮实现颜色选择器
- 背景选择器selector/shape
- shape 选择器 和selector
- 通过代码实现android shape的功能
- MVC
- Jmeter中引入class文件的方法
- C#繁体转简体批量工具(完整代码)
- Middle-题目88:31. Next Permutation
- Android消息机制
- 代码 实现按钮 seletor shape 选择器等
- SpringMVC、Zookeeper、Dubbo使用
- Java继承和多态
- Android中采用线程池进行线程的管理
- Android--从零单排系列(4)--常用对话框和DialogFragment的优势
- Spark安装
- Android中Textview加横线的属性
- Middle-题目89:229. Majority Element II
- Android API:自定义ViewGroup