[英]CustomView not showing inside ScrollView
New Android programmer here.新的 Android 程序员在这里。
I'm trying to display an .png image as a bitmap on Android.我正在尝试在 Android 上将 .png 图像显示为位图。 The only way I have been able to display the converted image at all is with a custom class that extends View
.我能够显示转换后的图像的唯一方法是使用扩展View
的自定义类。 However, my image is too large to be displayed entirely on the screen and I would like to be able to scroll with it.但是,我的图像太大而无法完全显示在屏幕上,我希望能够滚动它。 But when I define a ScrollView
and put the Canvas
with the Bitmap
into it, I get a blank screen.但是当我定义一个ScrollView
并将带有Bitmap
的Canvas
放入其中时,我得到了一个空白屏幕。 I had no luck setting this up with the layout file, so it is all done in the Activity class.我没有运气用布局文件设置它,所以这一切都是在 Activity 类中完成的。
This is where the Activity is created:这是创建活动的地方:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ScrollView scroll = new ScrollView(this);
scroll.addView(new CustomView(this));
setContentView(scroll);
}
And this is my CustomView class:这是我的 CustomView 类:
private class CustomView extends View{
public CustomView(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas){
Bitmap bitmapMap = BitmapFactory.decodeResource(getResources(),R.drawable.resourceimage);
canvas.drawBitmap(bitmapMap,0,0,null);
this.setWillNotDraw(false);
}
}
And if I replace this line of code in my Activity: setContentView(scroll)
with this line: setContentView(new CustomView(this))
, I can see the image, albeit not the entire image.如果我将 Activity: setContentView(scroll)
中的这行代码替换为这一行: setContentView(new CustomView(this))
,我可以看到图像,尽管不是整个图像。 So, is there a way to set this up in the layout files instead?那么,有没有办法在布局文件中进行设置呢? Or is there something I'm missing that I need to declare in the ScrollView
class?或者有什么我需要在ScrollView
类中声明的东西吗?
EDIT: I would prefer not to use ImageView, because I would like to change the image in specific locations, and using bitmap seemed to be the easiest way to accomplish that, via x and y coordinates.编辑:我不想使用 ImageView,因为我想更改特定位置的图像,并且使用位图似乎是通过 x 和 y 坐标实现这一目标的最简单方法。
您的自定义视图需要覆盖onMeasure
方法并正确设置测量的宽度和高度,以便父视图(在本例中为ScrollView
)可以知道为子视图分配多少空间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.