Android:如何在扩展Activity的类中使用onDraw方法? [英] Android: How to use the onDraw method in a class extending Activity?

查看:249
本文介绍了Android:如何在扩展Activity的类中使用onDraw方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为一个初学者,我一直在使用简单的布局xml和一个名为"Counter"的类构建一个简单的计数器应用程序,该类从Activity类派生(扩展).

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.

现在,我想加载一个位图(png文件)以放置在计数器旁边.我一直在阅读onDraw(),但是它要求该类扩展"View".我一直在尝试创建此类的对象来代替使用它,但无济于事.我对这个概念,如何轻松实现感到有些困惑.如果有人能解释,我将不胜感激.

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.

推荐答案

使用onDraw函数的简单示例-它需要类来扩展视图

Simple example using onDraw function-it requires class to extend view

上下文以获取当前活动上下文

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);
    }

}

}

这篇关于Android:如何在扩展Activity的类中使用onDraw方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆