创建自定义的ImageView [英] Creating Custom ImageView

查看:97
本文介绍了创建自定义的ImageView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建通过延伸ImageView的,只是在屏幕上绘制一些文字自定义图像观点,但是我没有看到任何东西涂在模拟器的屏幕,但日志消息和printlns得到印在日志控制台。我不能做什么?

这是我的活动

 公共类HelloAndroidActivity扩展活动
{
    / **第一次创建活动时调用。 * /
    @覆盖
    公共无效的onCreate(包savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        //的setContentView(R.layout.main);
        CustomImageView MyView的=新CustomImageView(getApplicationContext());
        的System.out.println(设置视图);
        myView.invalidate();
        的setContentView(MyView的);
        的System.out.println(调用无效);
    }
}
 

这是我的CustomImageView

 公共类CustomImageView扩展的ImageView
{

    / **
     * @参数方面
     * /
    公共CustomImageView(上下文的背景下)
    {
        超(上下文);
        // TODO自动生成构造函数存根
        setBackgroundColor(0XFFFFFF);
    }

    / **
     * @参数方面
     * @参数ATTRS
     * /
    公共CustomImageView(上下文的背景下,ATTRS的AttributeSet)
    {
        超(背景下,ATTRS);
        // TODO自动生成构造函数存根
    }

    / **
     * @参数方面
     * @参数ATTRS
     * @参数defStyle
     * /
    公共CustomImageView(上下文的背景下,ATTRS的AttributeSet,INT defStyle)
    {
        超(背景下,ATTRS,defStyle);
        // TODO自动生成构造函数存根
    }

    @覆盖
    保护无效的OnDraw(帆布油画)
    {
        // TODO自动生成方法存根
                super.onDraw(画布);
        的System.out.println(绘画的内容);
        涂料粉刷=新的油漆(Paint.LINEAR_TEXT_FLAG);
        paint.setColor(为0x0);
        paint.setTextSize(12.0F);
        的System.out.println(绘制文字);
        canvas.drawText(的Hello World在自定义视图,100,100,漆);
    }

    @覆盖
    公共布尔的onTouchEvent(MotionEvent事件)
    {
        // TODO自动生成方法存根
        Log.d(你好Android的,有一个触摸事件:+ event.getAction());
        返回super.onTouchEvent(事件);

    }
}
 

即使在的onTouchEvent()日志消息被打印出来,但没有什么是画。

这是我的main.xml中有布局

 < XML版本=1.0编码=UTF-8&GT?;

< AbsoluteLayout机器人:layout_width =FILL_PARENT机器人:layout_height =FILL_PARENT的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android机器人:ID =@ + ID / AbsoluteLayout&GT ;
< / AbsoluteLayout>
 

解决方案

使用颜色值 Col​​or.WHITE Col​​or.BLACK ,而不是六价值。

I create a custom image view by extending ImageView that just draws some text on the screen, however I don't see anything painted in the Emulator Screen, but the log messages and the printlns get printed in the log console. Am I not doing something?

This is my activity

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

This is my 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, 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);

    }
}

Even the log message in the onTouchEvent() gets printed, but nothing is painted.

This is my main.xml that has the layout

<?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>

解决方案

Use color values Color.WHITE or Color.BLACK instead of hexa values.

这篇关于创建自定义的ImageView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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