如何绘制嵌套循环编程 [英] how to draw nested circle programmatically
问题描述
在这里,我使用的图层列表中通过使用XML抽签嵌套环
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
下面是我的自定义视图
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);
}
}
感谢
推荐答案
在使用XML,规定的尺寸是DP - 密度无关的像素。但是,在你的code绘图功能,采取实际像素作为参数,你必须考虑到这一点,并计算出自己正确的价值观。
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.
根据设备屏幕申报密度1DP将被转换为:
Depending on your device declared screen density 1dp will be translated to:
- LDPI(120 DPI) - 0.75 PIX
- MDPI(160 DPI) - 1 PIX
- 华电国际(240 DPI) - 1.5 PIX
- 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 = 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);
}
}
您可以阅读更多:
- Supporting Multiple Screens
- Difference between px, dp, dip and sp in Android?
这篇关于如何绘制嵌套循环编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!