android:scaleType="fitStart"    图片靠左不变形显示,

android:scaleType=”fitEnd”  图片靠右显示,不变形.

半透明android:background="#e0000000"   透明 android:background="#00000000"

 

//  自定义TextView  圆角边框

package com.klgz.app.ui.widgets;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import com.klgz.app.gentleman.lt.R;

/**
* Created by Administrator on 2017/11/29.
* 自定义圆角图片
*
*/

public class ColorTextView extends View {

/**
* 文本内容
*/
private String mTitleText;
/**
* 文本的颜色
*/
private int mTitleTextColor;
/**
* 文本的大小
*/
private int mTitleTextSize;

private int ctvBackgroundColor;

/**
* 圆角大小
*/
private int mCornerSize;

/**
* 绘制时控制文本绘制的范围
*/
private Rect mtitleBound;
private Paint mtitlePaint;

public ColorTextView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}

public ColorTextView(Context context) {
this(context, null);
}

public void setCtvBackgroundColor(int ctvBackgroundColor) {
this.ctvBackgroundColor = ctvBackgroundColor;
}

/**
* 获得我自定义的样式属性
*
* @param context
* @param attrs
* @param defStyle
*/
public ColorTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);

/**
* 获得我们所定义的自定义样式属性
*/
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.ColorTextView, defStyle, 0);
int n = a.getIndexCount();
for (int i = 0; i < n; i++) {
int attr = a.getIndex(i);
switch (attr) {
case R.styleable.ColorTextView_ctvText:
mTitleText = a.getString(attr);
break;
case R.styleable.ColorTextView_ctvTextColor:
// 默认颜色设置为黑色
mTitleTextColor = a.getColor(attr, Color.BLACK);
break;
case R.styleable.ColorTextView_ctvTextSize:
// 默认设置为16sp,TypeValue也可以把sp转化为px
mTitleTextSize = a.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics()));
break;
case R.styleable.ColorTextView_ctvBackground:
//默认为白色
ctvBackgroundColor = a.getColor(attr, Color.WHITE);
break;
case R.styleable.ColorTextView_ctvCornerSize:
//默认圆角为0
mCornerSize = a.getInteger(attr, 0);
break;

}

}
a.recycle();
mtitlePaint = new Paint();
mtitlePaint.setTextSize(mTitleTextSize);
mtitleBound = new Rect();
mtitlePaint.getTextBounds(mTitleText, 0, mTitleText.length(), mtitleBound);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
int width;
int height;
if (widthMode == MeasureSpec.EXACTLY) {
width = widthSize;
} else {
mtitlePaint.setTextSize(mTitleTextSize);
mtitlePaint.getTextBounds(mTitleText, 0, mTitleText.length(), mtitleBound);

int desired = getPaddingLeft() + mtitleBound.width() + getPaddingRight();
width = desired <= widthSize ? desired : widthSize;
}

if (heightMode == MeasureSpec.EXACTLY) {
height = heightSize;
} else {
mtitlePaint.setTextSize(mTitleTextSize);
mtitlePaint.getTextBounds(mTitleText, 0, mTitleText.length(), mtitleBound);
int desired = getPaddingTop() + mtitleBound.height() + getPaddingBottom();
height = desired <= heightSize ? desired : heightSize;
}
setMeasuredDimension(width, height);
}

@Override
protected void onDraw(Canvas canvas) {

Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG);
paint.setAntiAlias(true);
paint.setColor(ctvBackgroundColor);
RectF rec = new RectF(0, 0, getMeasuredWidth(), getMeasuredHeight());
canvas.drawRoundRect(rec, mCornerSize, mCornerSize, paint);

mtitlePaint.setColor(mTitleTextColor);
Paint.FontMetricsInt fontMetrics = mtitlePaint.getFontMetricsInt();
int baseline = (getMeasuredHeight() - fontMetrics.bottom + fontMetrics.top) / 2 - fontMetrics.top;
canvas.drawText(mTitleText, getPaddingLeft(), baseline, mtitlePaint);
}
}


// 资源 文件









    
    android:id="@+id/t_sk"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"

radiostyle:ctvBackground="@color/zih"
radiostyle:ctvCornerSize="10"
radiostyle:ctvText="色"
radiostyle:ctvTextColor="@color/white"
radiostyle:ctvTextSize="15sp"
/>


//改变颜色方法
t_bj.setCtvBackgroundColor(getResources().getColor(R.color.huang));


 

转载于:https://www.cnblogs.com/dkqdream/p/7919532.html

更多相关文章

  1. android 加载图片到gallery
  2. android开发_文本按钮 与 输入框
  3. android 4.2修改设置菜单的背景颜色
  4. Android TextView文本的省略与显示
  5. Android 图片倒影和setXfermode
  6. Android控件笔记——在界面中显示及输入文本信息
  7. android TextView 文本过长时用滚动条显示
  8. Android新增AppCompatTextView自适应字体大小和文本宽度
  9. RadioButton修改标志图片

随机推荐

  1. Mac AndroidStudio真机调试
  2. 关于单选框RadioGroup和RadioButton
  3. Android引路蜂地图开发示例:第一个地图应
  4. android手机图片查看
  5. Android(安卓)- Looper.prepare()和Loope
  6. runONUIThread 分析与使用
  7. Android NDK之JNI使用例子
  8. Android之android:theme设置在Applicatio
  9. 深入了解android平台的jni(二)
  10. Android弹出软键盘布局是否上移问题