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);
}
} // 资源 文件
<!--自定义圆角换背景色色块-->
<declare-styleable name="ColorTextView">
<attr name="ctvText" format="string" />
<attr name="ctvBackground" format="color"/>
<attr name="ctvTextSize" format="dimension"/>
<attr name="ctvTextColor" format="color"/>
<attr name="ctvCornerSize" format="integer"/>
</declare-styleable>

<com.klgz.app.ui.widgets.ColorTextView
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));
												

Android Imageview 图片居左居右,自定义圆角的更多相关文章

  1. Css设置img属性让图片水平居中/居左/居右的写法

    图片的居中显示css有很多方法,但在很多情况下有的方法无效,无意发现这个系统的官方处理图片居中,居左,居右的css写法,喜欢的朋友可以收藏下哦 图片的居中显示css有很多方法,但在很多情况下有的方法无 ...

  2. asp.net,CSS设置<TableListView>的title居左,居左,居上

    居左 DIV.TableTitleStyle TABLE.grid TH { text-align:left; } 引用 <div class="TableTitleStyle&quo ...

  3. Xamarin.Android ImageView 图片圆角显示

    第一步:在 values 文件夹下新增 Attrs.xml 文件 <?xml version="1.0" encoding="utf-8" ?> & ...

  4. Android ImageView图片透明区域不响应点击事件,不规则图片透明区域响应点击事件

    转载:http://blog.csdn.net/aminfo/article/details/7872681 经常会在项目中用到透明图片,不规则图片,特别是做游戏的时候,需要对图片的透明区域的点击事件 ...

  5. Android ImageView图片自适应 (转)

    网络上下载下来的图片自适应:android:adjustViewBounds="true"(其详细解释在下面)<ImageView     android:id=" ...

  6. Android ImageView图片自适应

    网络上下载下来的图片自适应:android:adjustViewBounds="true"(其详细解释在下面) <ImageView     android:id=" ...

  7. Android imageView图片按比例缩放

    android:scaleType可控制图片的缩放方式,示例代码如下: <ImageView android:id="@+id/img" android:src=" ...

  8. xamarin.Android ImageView 图片圆角(自定义属性、扩展控件)

    新增 /values/Attrs.xml 文件 <?xml version="1.0" encoding="utf-8" ?> <resour ...

  9. Android ImageView 图片设置为透明

    方法一: 在xml中,设置如下 <ImageView            android:id="@+id/xx_id"            android:layout ...

随机推荐

  1. 极化码之tal-vardy算法(3)

    考完驾照,回来填坑 /doge/doge 前两节分别介绍了tal算法中的合并函数和信道操作两个部分,我们将高斯信道的应用放在最后一节来介绍. 在之前的介绍中,我们一直在一个前提下进行讨论--即输入字符 ...

  2. mysql5.7在windows不能启动的方法及查看数据库大小命令

    1.将mysql目录下的my-default.ini改为my.ini 2.cmd进入mysql的bin目录下 3.执行mysqld --initialize进行初始化(如果mysql目录下已经存在da ...

  3. Amaze UI 是一个移动优先的跨屏前端框架。 http://amazeui.org/

    http://amazeui.org/ Amaze UI 是一个移动优先的跨屏前端框架.... Amaze UI 以移动优先(Mobile first)为理念,从小屏逐步扩展到大屏,最终实现所有屏幕适 ...

  4. Python 3 使用venv创建虚拟环境

    Python 3.3以上使用venv来代替了原来Python2使用的virtualenv创建虚拟环境. 虚拟环境的作用是使得不同项目的Python包之间不会相互干扰,避免了由此产生的各种问题. 现在演 ...

  5. Windows系统下Nginx的安装与配置

    Nginx是lgor Sysoev在2004年的时候为俄罗斯访问量第二大的rambler.ru站点设计开发的,发布至今,凭借开源的力量,已经接近成熟与完善.其功能丰富,可作为HTTP服务器,也可作为反 ...

  6. MySQL show processlist说明

    html { font-family: sans-serif } body { margin: 0 } article,aside,details,figcaption,figure,footer,h ...

  7. Vue源码后记-其余内置指令(3)

    其实吧,写这些后记我才真正了解到vue源码的精髓,之前的跑源码跟闹着玩一样. go! 之前将AST转换成了render函数,跳出来后,由于仍是字符串,所以调用了makeFunction将其转换成了真正 ...

  8. linux学习(一)认识、安装Linux

    一.什么是Linux linux是一种操作系统,我们用的android和ios就是分别是linux操作系统和类unix操作系统. linux也是我们经常说的服务器.我们看的网站,游戏,app背后都是服 ...

  9. 有关CUBLAS中的矩阵乘法函数

    关于cuBLAS库中矩阵乘法相关的函数及其输入输出进行详细讨论. ▶ 涨姿势: ● cuBLAS中能用于运算矩阵乘法的函数有4个,分别是 cublasSgemm(单精度实数).cublasDgemm( ...

  10. javascript 可多选的下拉框 multiselect

    首先引用一个写的很好的博客http://www.cnblogs.com/landeanfen/p/5013452.html 我使用的是bootstrap-multiselect,实现功能是 选择下拉框 ...