Android:如何在扩展 Activity 的类中使用 onDraw 方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21221649/
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
Android: How to use the onDraw method in a class extending Activity?
提问by Turbosheep
As a beginner, I've been building a simple counter application using a simple layout xml and a class called 'Counter', which derives (extends) from the class Activity.
作为初学者,我一直在使用一个简单的布局 xml 和一个名为“Counter”的类构建一个简单的计数器应用程序,该类派生(扩展)自 Activity 类。
Now, I want to load a bitmap (png file) to place next to the counter. I've been reading up on onDraw(), but it requires the class to extend 'View'. I've been trying to create an object of this class to use this instead, to no avail. I'm a bit stumped about this concept, how to do it easily. If anyone could explain, I'd appreciate it.
现在,我想加载一个位图(png 文件)以放置在计数器旁边。我一直在阅读 onDraw(),但它需要该类来扩展“视图”。我一直在尝试创建此类的对象来使用它,但无济于事。我对这个概念有点困惑,如何轻松地做到这一点。如果有人可以解释,我将不胜感激。
回答by venugopal
Simple example using onDraw function-it requires class to extend view
使用 onDraw 函数的简单示例 - 它需要类来扩展视图
Context to get the current activity context
获取当前活动上下文的上下文
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new myview(this));
}
class myview extends View
{
public myview(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
int x=80;
int y=80;
int radius=40;
Paint paint=new Paint();
// Use Color.parseColor to define HTML colors
paint.setColor(Color.parseColor("#CD5C5C"));
canvas.drawCircle(x, y, radius, paint);
}
}
}
回答by Gagandeep
You don't need to use View.onDraw()
for this purpose.
Just use layouts and place any drawable image next to your counter, let's say a textview, using the drawableLeft
attribute.
您不需要View.onDraw()
为此目的使用。只需使用布局并将任何可绘制图像放置在您的计数器旁边,例如使用drawableLeft
属性的 textview 。
txtview_counter.drawableLeft="@drawable/xxx"
txtview_counter.drawableLeft="@drawable/xxx"
回答by Android
I think that you cant use onDraw in class extending an Activity because of using Canvas, you need to create a Custom component extending some View and there handle you action,and what do you need to do that=>
我认为你不能在类中使用 onDraw 因为使用 Canvas 而扩展 Activity,你需要创建一个自定义组件扩展一些 View 并在那里处理你的动作,你需要做什么=>
1.Extend an existing View class or subclass with your own class.
1.使用您自己的类扩展现有的 View 类或子类。
2.Override some of the methods from the superclass. The superclass methods to override start with ‘on', for example,onDraw(), onMeasure(), onKeyDown(). This is similar to the‘on' events in Activity that you override for lifecycle and other functionality hooks.
2.覆盖超类中的一些方法。要覆盖的超类方法以“on”开头,例如,onDraw()、onMeasure()、onKeyDown()。这类似于您为生命周期和其他功能挂钩覆盖的 Activity 中的“on”事件。
3.Use your new extension class. Once completed, your new extension class can be used in place of the view upon which it was based.
3.使用你的新扩展类。完成后,您的新扩展类可以用来代替它所基于的视图。
回答by mr_hyde
Couldn't you just place an ImageView next to your counter? You could do that in your xml layout file:
你不能在你的柜台旁边放一个 ImageView 吗?您可以在 xml 布局文件中执行此操作:
<ImageView
android:id="@id+/my_image"
android:layout_width="50dip"
android:layout_height="50dip"
android:src="@drawable/name_of_my_image" />
Or in your acrivity:
或者在您的活动中:
@Override
protected void onCreate(Bundle bundle) {
// ...
ImageView image = (ImageView) findViewById(R.id.my_image);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.my_image);
image.setImageBitmap(bitmap);
}
If you want to use a custom view and override the onDraw() method here's what you need to do:
如果您想使用自定义视图并覆盖 onDraw() 方法,您需要执行以下操作:
class MyCustomView extends View {
public MyCustomView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onDraw(Canvas canvas) {
// your custom drawing
canvas.drawRect(0, 0, 50, 50, new Paint());
}
}
For any doubt, you can refer to Android Training
有任何疑问,可以参考Android Training