gpt4 book ai didi

android - 创建自定义 ImageView

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:11:29 28 4
gpt4 key购买 nike

我通过扩展仅在屏幕上绘制一些文本的 ImageView 创建自定义 ImageView ,但是我在模拟器屏幕上看不到任何绘制的内容,但日志消息和 printlns 打印在日志控制台中。我不是在做什么吗?

这是我的 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");
}
}

这是我的 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>

最佳答案

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

关于android - 创建自定义 ImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1470867/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com