在 Android 中实现自定义 drawable
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2967904/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Implementing a customized drawable in Android
提问by Girish
I was trying to get hold of 2D graphics in Android. As a example i want to implement a custom drawable and show it in my Activity
我试图在 Android 中获取 2D 图形。作为一个例子,我想实现一个自定义 drawable 并在我的 Activity 中显示它
I have defined a customized drawable by extending from Android drawable as mentioned below
我通过从 Android drawable 扩展定义了一个自定义的 drawable,如下所述
class myDrawable extends Drawable {
private static final String TAG = myDrawable.class.getSimpleName();
private ColorFilter cf;
@Override
public void draw(Canvas canvas) {
//First you define a colour for the outline of your rectangle
Paint rectanglePaint = new Paint();
rectanglePaint.setARGB(255, 255, 0, 0);
rectanglePaint.setStrokeWidth(2);
rectanglePaint.setStyle(Style.FILL);
//Then create yourself a Rectangle
RectF rectangle = new RectF(15.0f, 50.0f, 55.0f, 75.0f); //in pixels
Log.d(TAG,"On Draw method");
// TODO Auto-generated method stub
Paint paintHandl = new Paint();
// paintHandl.setColor(0xaabbcc);
paintHandl.setARGB(125, 234, 213, 34 );
RectF rectObj = new RectF(5,5,25,25);
canvas.drawRoundRect(rectangle, 0.5f, 0.5f, rectanglePaint);
}
@Override
public int getOpacity() {
// TODO Auto-generated method stub
return 100;
}
@Override
public void setAlpha(int alpha) {
// TODO Auto-generated method stub
}
@Override
public void setColorFilter(ColorFilter cf) {
// TODO Auto-generated method stub
this.cf = cf;
}
}
I am trying to get this displayed in my activity, as shown below
我正在尝试将其显示在我的活动中,如下所示
public class custDrawable extends Activity {
/** Called when the activity is first created. */
LinearLayout layObj = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
layObj = (LinearLayout) findViewById(R.id.parentLay);
ImageView imageView = (ImageView) findViewById(R.id.icon2);
myDrawable myDrawObj = new myDrawable();
imageView.setImageDrawable(myDrawObj);
imageView.invalidate();
// layObj.addView(myDrawObj, params);
}
}
But when i run the app i see no rectangle on the activity, can anyone help me out? Where am i going wrong?
但是当我运行应用程序时,我在活动中看不到矩形,有人可以帮我吗?我哪里出错了?
回答by CaseyB
Your problem is in the getOpacity()
method. 100 is not a valid value. You should use a PixelFormatvalue. Also, you should create your RectF
and Paint
in the constructor and then just adjust the values in draw()
so you don't create so many objects that need garbage collected. Like this:
你的问题出在getOpacity()
方法上。100 不是有效值。您应该使用PixelFormat值。此外,您应该在构造函数中创建您的RectF
和Paint
,然后调整其中的值,draw()
这样您就不会创建太多需要垃圾收集的对象。像这样:
public class Square extends Drawable
{
private final Paint mPaint;
private final RectF mRect;
public Square()
{
mPaint = new Paint();
mRect = new RectF();
}
@Override
public void draw(Canvas canvas)
{
// Set the correct values in the Paint
mPaint.setARGB(255, 255, 0, 0);
mPaint.setStrokeWidth(2);
mPaint.setStyle(Style.FILL);
// Adjust the rect
mRect.left = 15.0f;
mRect.top = 50.0f;
mRect.right = 55.0f;
mRect.bottom = 75.0f;
// Draw it
canvas.drawRoundRect(mRect, 0.5f, 0.5f, mPaint);
}
@Override
public int getOpacity()
{
return PixelFormat.OPAQUE;
}
@Override
public void setAlpha(int arg0)
{
}
@Override
public void setColorFilter(ColorFilter arg0)
{
}
}
回答by Joe Plante
You may have to implement other overrides like getIntrinsicWidth
and getIntrinsicHeight
. One way to tell is that you set your layout_width and layout_height
to some constant (layout_width="42dip"
layout_height="42dip"
in XML or setting your layoutParams
to some value if you are using Java layouts). Some View
types handle not having getIntrinsic*
implemented than others, so try them! This includes a straight View
.
您可能需要实现其他覆盖,例如getIntrinsicWidth
和getIntrinsicHeight
。一种判断方法是将 layout_width 设置layout_height
为某个常量(layout_width="42dip"
layout_height="42dip"
在 XML 中,或者layoutParams
如果使用 Java 布局,则将其设置为某个值)。有些View
类型处理没有getIntrinsic*
实现比其他类型,所以尝试它们!这包括一个直线View
。
You can try returning -1 if there's no specific width or height.
如果没有特定的宽度或高度,您可以尝试返回 -1。
Hard to tell if the issue ever got resolved, but I got here via Google trying to help myself remember the details of making a custom Drawable
, plus I want to help people avoid this: http://xkcd.com/979/
很难说问题是否得到解决,但我通过谷歌来到这里,试图帮助自己记住定制的细节Drawable
,另外我想帮助人们避免这种情况:http: //xkcd.com/979/