android – 创建自定义ImageView

我创建一个自定义图像视图,通过扩展 ImageView,只是在屏幕上绘制一些文本,但是我没有看到任何在仿真器屏幕上绘制的东西,但日志信息和printlns打印在日志控制台中.我不做点什么

这是我的活动

public class HelloAndroidActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        //        setContentView(R.layout.main);
        CustomImageView myView = new CustomImageView(getApplicationContext());
        System.out.println("Setting the view");
        myView.invalidate();
        setContentView(myView);
        System.out.println("Calling invalidate");
    }
}

这是我的CustomImageView

public class CustomImageView extends ImageView
{

    /**
     * @param context
     */
    public CustomImageView(Context context)
    {
        super(context);
        // Todo Auto-generated constructor stub
        setBackgroundColor(0xFFFFFF);
    }

    /**
     * @param context
     * @param attrs
     */
    public CustomImageView(Context context,AttributeSet attrs)
    {
        super(context,attrs);
        // Todo Auto-generated constructor stub
    }

    /**
     * @param context
     * @param attrs
     * @param defStyle
     */
    public CustomImageView(Context context,AttributeSet attrs,int defStyle)
    {
        super(context,attrs,defStyle);
        // Todo Auto-generated constructor stub
    }

    @Override
    protected void onDraw(Canvas canvas)
    {
        // Todo Auto-generated method stub
                super.onDraw(canvas);
        System.out.println("Painting content");
        Paint paint  = new Paint(Paint.LINEAR_TEXT_FLAG);
        paint.setColor(0x0);
        paint.setTextSize(12.0F);
        System.out.println("Drawing text");
        canvas.drawText("Hello World in custom view",100,paint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
        // Todo Auto-generated method stub
        Log.d("Hello Android","Got a touch event: " + event.getAction());
        return super.onTouchEvent(event);

    }
}

即使打印onTouchEvent()中的日志消息,也没有打印任何内容.

这是我的main.xml的布局

<?xml version="1.0" encoding="utf-8"?>

<AbsoluteLayout android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/AbsoluteLayout">
</AbsoluteLayout>

解决方法

使用颜色值Color.WHITE或Color.BLACK而不是六进制值.

相关文章

这篇“android轻量级无侵入式管理数据库自动升级组件怎么实现...
今天小编给大家分享一下Android实现自定义圆形进度条的常用方...
这篇文章主要讲解了“Android如何解决字符对齐问题”,文中的...
这篇文章主要介绍“Android岛屿数量算法怎么使用”的相关知识...
本篇内容主要讲解“Android如何开发MQTT协议的模型及通信”,...
本文小编为大家详细介绍“Android数据压缩的方法是什么”,内...