问题描述
这里我使用图层列表来绘制嵌套的循环使用XML
<项目>
< shape android:shape =oval>
< stroke
android:width =1dp
android:color =@ android:color / holo_orange_light/>
< padding
android:bottom =7dp
android:left =7dp
android:right =7dp
android: top =7dp/>
< / shape>
< / item>
< item>
< shape android:shape =oval>
< solid android:color =@ color / welcome_bg/>
< / shape>
< / item>
否我想通过编程方式使用相同的嵌套循环,因为我想要动态更改颜色,有没有办法在上面提供的xml中动态更改颜色
这是我的自定义视图
public class MyView extends EditText {
public MyView(Context context,AttributeSet attrs,int defStyleAttr){
super(context,attrs,defStyleAttr);
}
public MyView(Context context,AttributeSet attrs){
super(context,attrs);
}
public MyView(Context context){
super(context);
}
@Override
protected void onDraw(Canvas canvas){
绘制油漆=新的Paint();
paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.GRAY);
RectF oval1 = new RectF(50,50,300,300);
canvas.drawOval(oval1,paint);
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.RED);
RectF oval2 = new RectF(55,55,295,295);
canvas.drawOval(oval2,paint);
}
}
谢谢
当您使用xml时,指定的维度在dp - 密度无关的像素中。但是在代码绘制函数中,实际的像素作为参数,您必须考虑到这一点,并自行计算适当的值。
根据您的设备声明的屏幕密度,1dp将翻译成:
- ldpi(120 dpi) - 0.75 pix
- mdpi(160 dpi) - 1像素
- hdpi(240 dpi) - 1.5像素
- xhdpi(320 dpi) - 2 pix
- xxhdpi(480 dpi) - 3 pix
- xxxhdpi(640 dpi) - 4 pix
计算真实像素的公式是 px = dp *(dpi / 160)
public class MyView extends EditText {
float mDensityScale;
public MyView(Context context,AttributeSet attrs,int defStyleAttr){
super(context,attrs,defStyleAttr);
init(context,attrs,defStyleAttr);
}
public MyView(Context context,AttributeSet attrs){
super(context,attrs);
init(context,attrs,0);
}
public MyView(Context context){
super(context);
init(context,null,0);
}
private void init(Context context,AttributeSet attrs,int defStyle)
{
final DisplayMetrics dm = context.getResources()。getDisplayMetrics();
mDensityScale = dm.density;
}
private float pix(float dp)
{
return dp * mDensityScale;
}
@Override
protected void onDraw(Canvas canvas){
绘制油漆=新的Paint();
paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.GRAY);
RectF oval1 = new RectF(pix(50),pix(50),pix(300),pix(300));
canvas.drawOval(oval1,paint);
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.RED);
RectF oval2 = new RectF(pix(55),pix(55),pix(295),pix(295));
canvas.drawOval(oval2,paint);
}
}
您可以阅读更多: p>
Here I am using layer list to draw nested Circle by using XML
<item>
<shape android:shape="oval" >
<stroke
android:width="1dp"
android:color="@android:color/holo_orange_light" />
<padding
android:bottom="7dp"
android:left="7dp"
android:right="7dp"
android:top="7dp" />
</shape>
</item>
<item>
<shape android:shape="oval" >
<solid android:color="@color/welcome_bg" />
</shape>
</item>
No I want same nested circle by using programmatically because I want to change color dynamically or is there any way to change color dynamically in xml provided above
Here is my custom View
public class MyView extends EditText {
public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyView(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
Paint paint = new Paint();
paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.GRAY);
RectF oval1 = new RectF(50, 50, 300, 300);
canvas.drawOval(oval1, paint);
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.RED);
RectF oval2 = new RectF(55, 55, 295, 295);
canvas.drawOval(oval2, paint);
}
}
Thanks
When you use xml, specified dimensions are in dp - density independent pixels. But in your code drawing functions take actual pixels as parameters and you have to take that into account and calculate proper values yourself.
Depending on your device declared screen density 1dp will be translated to:
- ldpi (120 dpi) - 0.75 pix
- mdpi (160 dpi) - 1 pix
- hdpi (240 dpi) - 1.5 pix
- xhdpi (320 dpi) - 2 pix
- xxhdpi (480 dpi) - 3 pix
- xxxhdpi (640 dpi) - 4 pix
formula for calculating real pixels is px = dp * (dpi / 160)
public class MyView extends EditText {
float mDensityScale;
public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs, defStyleAttr);
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs, 0);
}
public MyView(Context context) {
super(context);
init(context, null, 0);
}
private void init(Context context, AttributeSet attrs, int defStyle)
{
final DisplayMetrics dm = context.getResources().getDisplayMetrics();
mDensityScale = dm.density;
}
private float pix(float dp)
{
return dp * mDensityScale;
}
@Override
protected void onDraw(Canvas canvas) {
Paint paint = new Paint();
paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.GRAY);
RectF oval1 = new RectF(pix(50), pix(50), pix(300), pix(300));
canvas.drawOval(oval1, paint);
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.RED);
RectF oval2 = new RectF(pix(55), pix(55), pix(295), pix(295));
canvas.drawOval(oval2, paint);
}
}
You can read more:
这篇关于如何以编程方式绘制嵌套的圆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!