Android应用开发小知识点

时间:2021-12-22 14:43:02

1- TextView 动态设置粗体

TextView textView = (TextView)findViewById(R.id.textView);
TextPaint tp = textView .getPaint();
tp.setFakeBoldText(true);


2- TextView 动态设置字体大小

setTextSize(int unit, int size)
TypedValue.COMPLEX_UNIT_PX : Pixels
TypedValue.COMPLEX_UNIT_SP : Scaled Pixels
TypedValue.COMPLEX_UNIT_DIP : Device Independent Pixels


3- TextView 边框圆角显示

android:background="@drawable/round_corner"

在drawable目录下,xml文件如下:

<shape android:shape="rectangle">
<stroke android:color="#d34940" android:width="1dp"/> 边框,颜色
<solid android:color="@color/white"/> 填充颜色
<corners android:radius="30dp"/> 角度
<size android:height="30dp"/> 高度
</shape>

4- TextView 放在圆圈里显示

宽和高要相等

android:layout_width="30dp"
android:layout_height="30dp"
android:background="@drawable/round"

在drawable目录下,xml文件如下:

<shape android:shape="oval">
<stroke android:color="#d34940" android:width="1dp"/> 边框,颜色
<solid android:color="#00000000"/> 填充颜色
</shape>


5- ImageView 变圆处理

public static Bitmap toRoundBitmap(Bitmap bitmap) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
float roundPx;
float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom;
if (width <= height) {
roundPx = width / 2;
left = 0;
top = 0;
right = width;
bottom = width;
height = width;
dst_left = 0;
dst_top = 0;
dst_right = width;
dst_bottom = width;
} else {
roundPx = height / 2;
float clip = (width - height) / 2;
left = clip;
right = width - clip;
top = 0;
bottom = height;
width = height;
dst_left = 0;
dst_top = 0;
dst_right = height;
dst_bottom = height;
}

Bitmap output = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Canvas canvas = new Canvas(output);

final int color = 0xff424242;
final Paint paint = new Paint();
final Rect src = new Rect((int) left, (int) top, (int) right,(int) bottom);
final Rect dst = new Rect((int) dst_left, (int) dst_top,(int) dst_right, (int) dst_bottom);
final RectF rectF = new RectF(dst);
paint.setAntiAlias(true);// 设置画笔无锯齿
canvas.drawARGB(0, 0, 0, 0); // 填充整个Canvas
// paint.setAlpha(220);
paint.setColor(0xFFE24200);

// 以下有两种方法画圆,drawRounRect和drawCircle
// canvas.drawRoundRect(rectF, roundPx, roundPx, paint);//
// 画圆角矩形,第一个参数为图形显示区域,第二个参数和第三个参数分别是水平圆角半径和垂直圆角半径。
canvas.drawCircle(roundPx, roundPx, roundPx, paint);

paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));// 设置两张图片相交时的模式,参考
canvas.drawBitmap(bitmap, src, dst, paint); // 以Mode.SRC_IN模式合并bitmap和已经draw了的Circle

return output;
}