创建自定义 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, 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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用颜色值 Color.WHITE 或 Color.BLACK 而不是十六进制值。
Use color values Color.WHITE or Color.BLACK instead of hexa values.
你检查过你的画布尺寸吗?图像视图期望位图/可绘制对象返回其大小,并根据缩放类型标志确定视图的大小。我在您的代码中没有看到任何确定布局需求的视图大小的内容。
-瑞克
Have you checked your canvas size? An image view expects the bitmap/drawable to return its size and based on the scaletype flags, determine the size of the view. I don't see anything in your code that determines the size of the view for layout needs.
-Rick