2009-09-24 39 views
10

我通过扩展ImageView来创建自定义图像视图,只是在屏幕上绘制了一些文本,但是我没有在仿真器屏幕上看到任何内容,但日志消息和printlns打印在日志控制台中。我没做什么?创建自定义ImageView

这是我的活动

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

回答

3

使用颜色值Color.WHITEColor.BLACK,而不是六值。

+0

不,它不起作用 – Ram 2009-09-24 12:02:25

+0

@Ram - 唯一的问题是颜色值。我们应该使用Color类生成颜色值。 – bhatt4982 2009-09-24 12:05:59

+2

或使用格式为0xAARRGGBB的颜色十六进制值。 白= 0xFFFFFFFF和黑= 0xFF000000 – bhatt4982 2009-09-24 13:05:51

1

您是否检查过您的画布尺寸?图像视图期望位图/可绘制返回其大小并基于scaletype标志确定视图的大小。我没有看到代码中的任何内容决定了布局需求的视图大小。

-Rick