当前位置:网站首页>(original) custom drawable
(original) custom drawable
2022-06-27 08:18:00 【Android_ xiong_ st】
Preface
In everyday android In development , We will use it often drawable resources
Setting up icon:
imageView.setImageDrawable(R.drawable.ic_launcher);
Let's see what is Drawable:
General drawable We are all one picture
But in fact, we can also customize View equally
To customize our Drawable
Self defined Drawable
You can also draw text , shape , And add some pictures
Be what you need
This article will teach you how to customize a Drawable
Effect analysis
Let's take a look at the renderings first :
We can see this customized drawable There are several characteristics :
1: The background should be a cut , But the above text was drawn by myself
2: The text on the left has a stroke effect
3: The position of the two words , The one on the left is in the middle of love , The words on the right lean against the right of love
Let's take this effect step by step
Implementation process
First, we customize a class to inherit Drawable class :
public class MedalDrawable extends Drawable {
@Override
public void draw(@NonNull Canvas canvas) {
}
@Override
public void setAlpha(int alpha) {
}
@Override
public void setColorFilter(@Nullable ColorFilter colorFilter) {
}
@Override
public int getOpacity() {
return 0;
}
}
You can see several methods to rewrite :
draw: Drawing method , Naturally, there is no need to say , And customization View The drawing of is the same , The relevant effects are drawn here
setAlpha: Set up Drawable Transparency , Generally, we will pass this transparency to the paintbrush , Or do not deal with it
setColorFilter: Set up a color filter , So before you draw it , Each pixel of the drawn content is changed by the color filter
getOpacity: Get opacity , Its value can be determined according to setAlpha Adjust the value set in . such as ,
alpha == 0 Is set to PixelFormat.TRANSPARENT
stay alpha == 255 Is set to PixelFormat.OPAQUE;
Set to at other times PixelFormat.TRANSLUCENT
PixelFormat.OPAQUE: Is completely opaque , Everything under him
PixelFormat.TRANSPARENT: transparent , Nothing at all
PixelFormat.TRANSLUCENT: Only the drawing places cover the contents below
There are two other methods :
getIntrinsicWidth
getIntrinsicHeight
Get internal width and height , Mainly in order to View Use wrap_content When , The size returned using these two methods
Next, let's look at our specific implementation :
public MedalDrawable(int resId, String mText, Context mContext) {
this.mContext=mContext;
mBitmap = BitmapFactory.decodeResource(mContext.getResources(), resId);
this.mText = mText;
setBounds(0, 0, getIntrinsicWidth(), getIntrinsicHeight());
initPaint();// Initialize medal brush
initThirdPaint();// Initialize medal level brush
}
To draw bitmap, First, you need to pass in a resource id, Then came the text to be drawn on the right
The numbers on the left are written to death when we demonstrate here
Then it is to set drawable And initialize the brush
You need to initialize two kinds of brushes , Draw the left and right text respectively , A stroked effect , One without stroke effect
Finally, let's talk about how to achieve the stroke effect
In fact, the principle is very simple
Is to draw a text with a thicker stroke below
Then draw a relatively thin stroke on the same surface
The method used is to set the brush stroke thickness :
mStrokePaint.setStrokeWidth(4); // Set the stroke width
As for the text placement
It is set when drawing text
Finally, let's look at the source code
Source code
public class MedalDrawable extends Drawable {
private Bitmap mBitmap;
private String mText;
protected Context mContext;
Paint.FontMetricsInt fontMetrics;
int marginLeft = 62;// Text distance left margin
int strokeMarginLeft = 0;// Stroke text distance left margin
int textSize = 20;// Text size
private Paint mPaint;
private Rect bounds;
private String mThirdText="21";
private Paint mThirdPaint;// Medal Rank number
private Paint mStrokePaint;// Word stroke
Paint.FontMetricsInt mThridFontMetrics;
private int strokeBaseline;
public MedalDrawable(int resId, String mText, Context mContext) {
this.mContext=mContext;
mBitmap = BitmapFactory.decodeResource(mContext.getResources(), resId);
this.mText = mText;
setBounds(0, 0, getIntrinsicWidth(), getIntrinsicHeight());
initPaint();// Initialize medal brush
initThirdPaint();// Initialize medal level brush
}
private void initThirdPaint() {
mThirdPaint = new Paint();
mThirdPaint.setColor(Color.parseColor("#FFFFFF"));
mThirdPaint.setFakeBoldText(true);
mThirdPaint.setAntiAlias(true);
mThirdPaint.getTextBounds(mThirdText, 0, mThirdText.length(), new Rect());
mStrokePaint = new Paint();
mStrokePaint.setColor(Color.parseColor("#bf11e6"));
mStrokePaint.setFakeBoldText(true);
mStrokePaint.setAntiAlias(true);
mStrokePaint.setStrokeWidth(4); // Set the stroke width
mStrokePaint.setStyle(Paint.Style.STROKE);
mStrokePaint.getTextBounds(mThirdText, 0, mThirdText.length(), new Rect());
if (mThirdText.length() >= 2) {
mThirdPaint.setTextSize(mContext.getResources().getDimensionPixelSize(R.dimen.text_size_6));
mStrokePaint.setTextSize(mContext.getResources().getDimensionPixelSize(R.dimen.text_size_6));
} else {
mThirdPaint.setTextSize(mContext.getResources().getDimensionPixelSize(R.dimen.text_size_9));
mStrokePaint.setTextSize(mContext.getResources().getDimensionPixelSize(R.dimen.text_size_9));
}
mThridFontMetrics = mThirdPaint.getFontMetricsInt();
}
private void initPaint() {
mPaint = new Paint();
mPaint.setColor(Color.parseColor("#FFFFFF"));
mPaint.setFakeBoldText(true);
mPaint.setAntiAlias(true);
bounds= new Rect();
mPaint.getTextBounds(mText, 0, mText.length(), bounds);
mPaint.setTextSize(textSize);
fontMetrics = mPaint.getFontMetricsInt();
}
@Override
public void draw(@NonNull Canvas canvas) {
// Draw medal text
int baseline = (getIntrinsicHeight() - fontMetrics.bottom + fontMetrics.top) / 2 - fontMetrics.top;
canvas.drawBitmap(mBitmap, 0, 0, null);
canvas.drawText(mText, marginLeft, baseline, mPaint);
// Draw stroke text
strokeBaseline = (getIntrinsicHeight() - mThridFontMetrics.bottom + mThridFontMetrics.top) / 2 - mThridFontMetrics.top;
strokeMarginLeft = mContext.getResources().getDimensionPixelOffset(R.dimen.dp_5);
if (mThirdText.length() == 1) {
strokeMarginLeft = mContext.getResources().getDimensionPixelOffset(R.dimen.dp_8);
} else if (mThirdText.length() == 2) {
strokeMarginLeft = mContext.getResources().getDimensionPixelOffset(R.dimen.dp_7);
} else if (mThirdText.length() == 3) {
strokeMarginLeft = mContext.getResources().getDimensionPixelOffset(R.dimen.dp_1);
}
canvas.drawText(mThirdText, strokeMarginLeft, strokeBaseline, mStrokePaint);
canvas.drawText(mThirdText, strokeMarginLeft, strokeBaseline, mThirdPaint);
}
@Override
public void setAlpha(int alpha) {
}
@Override
public void setColorFilter(@Nullable ColorFilter colorFilter) {
}
@Override
public int getOpacity() {
return PixelFormat.UNKNOWN;
}
@Override
public int getIntrinsicWidth() {
return mBitmap.getWidth();
}
@Override
public int getIntrinsicHeight() {
return mBitmap.getHeight();
}
}
边栏推荐
- L'introduction en bourse de Wild Wind Pharmaceutical a pris fin: Yu pinzeng, qui avait l'intention de lever 540 millions de RMB, a effectué un investissement P2P.
- Online text digit recognition list summation tool
- Code source AQS sous - jacent pour la programmation simultanée juc
- 【10. 差分】
- DataV轮播表组件dv-scroll-board宽度问题
- ACM course term summary
- 【批处理DOS-CMD命令-汇总和小结】-cmd的内部命令和外部命令怎么区分,CMD命令和运行(win+r)命令的区别,
- Closure problem
- JVM常见的垃圾收集器
- Install Jenkins
猜你喜欢

All tutor information on one page
![【批处理DOS-CMD命令-汇总和小结】-批处理命令中的参数%0、%1、%2、%[0-9]、%0-9和批处理命令参数位置切换命令shift,dos命令中操作符%用法](/img/05/19299c47d54d4ede95322b5a923093.png)
【批处理DOS-CMD命令-汇总和小结】-批处理命令中的参数%0、%1、%2、%[0-9]、%0-9和批处理命令参数位置切换命令shift,dos命令中操作符%用法

准备好迁移上云了?请收下这份迁移步骤清单

淘宝虚拟产品开店教程之作图篇
![[batch dos-cmd command - summary and summary] - map folder to virtual disk - subst](/img/09/cd12c276392d3465dce1909d0f86a6.png)
[batch dos-cmd command - summary and summary] - map folder to virtual disk - subst

即构「畅直播」,全链路升级的一站式直播服务

MySQL lock details

野風藥業IPO被終止:曾擬募資5.4億 實控人俞蘠曾進行P2P投資

05 observer mode

Game asset reuse: a new way to find required game assets faster
随机推荐
Redis配置文件详解
(note) Anaconda navigator flashback solution
【批处理DOS-CMD命令-汇总和小结】-环境变量、路径变量、搜索文件位置相关指令——set、path、where,cmd命令的路径参数中有空格怎么办
游戏六边形地图的实现
分析日志.log
"Short video" Linxia fire rescue detachment carries out fire safety training
【12. 最大连续不重复子序列】
L'introduction en bourse de Wild Wind Pharmaceutical a pris fin: Yu pinzeng, qui avait l'intention de lever 540 millions de RMB, a effectué un investissement P2P.
【11. 二维差分】
c#的初步认识
参考 | Win11 开启热点之后电脑不能上网
LVGL GUI GUIDER移植代码到STM32
八大误区,逐个击破(终篇):云难以扩展、定制性差,还会让管理员失去控制权?
What is futures reverse documentary?
AQS underlying source code of concurrent programming JUC
Experience record of Luogu's topic brushing
野風藥業IPO被終止:曾擬募資5.4億 實控人俞蘠曾進行P2P投資
C how to call line and rows when updating the database
2022.06.26(LC_6100_统计放置房子的方式数)
If xn > 0 and X (n+1) /xn > 1-1/n (n=1,2,...), Prove that the series Σ xn diverges