创建自定义ImageView
-
16-09-2019 - |
题
我通过扩展图像视图来创建自定义图像视图,该图像视图仅在屏幕上绘制一些文本,但是我看不到模拟器屏幕上绘制的任何内容,但是日志消息和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()中的日志消息也会打印出来,但没有绘制。
这是我的主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>
解决方案
使用颜色值 白颜色 或者 颜色:黑色 而不是Hexa值。
其他提示
您检查了帆布尺寸吗?图像视图期望绘制的位图/可绘制其大小并基于ScaleType标志,确定视图的大小。我在代码中看不到任何确定布局需求视图大小的任何内容。
- 里克
不隶属于 StackOverflow