Android:我如何用图形覆盖ImageView?
抱歉,我的问题完全是初学者的问题,但我在这里撕扯我的头发。。。 我有一个简单的视图,在ImageView背景上有几个小部件。覆盖一切是一个透明的图像视图,我想在上面画各种文字和图形。目的是在透明的ImageView2上绘制图形 main.xmlAndroid:我如何用图形覆盖ImageView?,android,Android,抱歉,我的问题完全是初学者的问题,但我在这里撕扯我的头发。。。 我有一个简单的视图,在ImageView背景上有几个小部件。覆盖一切是一个透明的图像视图,我想在上面画各种文字和图形。目的是在透明的ImageView2上绘制图形 main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:scaleType="fitXY"
android:src="@drawable/background" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:ems="10"
android:inputType="numberDecimal" />
<Button
android:id="@+id/button1"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="19dp"
android:text="Button" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:scrollbarAlwaysDrawVerticalTrack="false"
android:src="@drawable/transparent" />
</RelativeLayout>
DrawView.java:
package com.example.overlay;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;
public class DrawView extends View {
Paint paint = new Paint();
public DrawView(Context context) {
super(context);
}
@Override
public void onDraw(Canvas canvas) {
int w = getWidth(), h=getHeight();
paint.setColor(Color.GREEN);
paint.setTextSize(20);
canvas.drawText("Android", w/3, h/2, paint);
}
}
甚至对我来说,这显然是行不通的,因为我还没有将onDraw()方法与透明覆盖imageView2相关联。
如果我使用“setContentView(drawView);”然后我松开背景等等,在白色背景下。。。如果我使用“setContentView(R.layout.main);”那我就听不懂课文了。
所以问题是,如何将透明ImageView与onDraw()方法关联?
这是最好的方法吗?
我已经在谷歌上搜索了好几天;也许我只是不明白该问什么问题:)我想内森在他的评论中提到了这一点 您需要将自定义
DrawView
添加到xml布局中,以便:
<com.example.overlay.DrawView
android:id="@+id/overlay"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY"/>
然后,您可以像已经做的那样重写onDraw
方法,然后可以操作画布
确保将Canvas
传递到onDraw
方法时,它是使用getWidth()
和getHeight()方法的屏幕宽度和高度。如果不是,则可以从新的位图设置画布,然后在画布上进行绘制
在Android开发者网站上有一篇关于创建自定义视图的有用文章,这是一本不错的读物。您可以找到它。您需要将DrawView添加到layout.xml。然后调用setContentView(R.layout.main);不要使用像ImageView这样的内置视图,而是将ImageView替换为您的类名(您可能必须提供完整的类路径com.your.app.DrawView)。感谢您的回复,但是,正如我所说,我是一个完全(几乎没有)的初学者!有没有可能写点代码?我尝试过“但是Eclipse在推特”自定义视图DrawView没有使用2参数或3参数视图构造函数;“XML属性不起作用”,我完全不知道这意味着什么。无论如何,应用程序都会在模拟器中崩溃。现在有件事。。。将main.xml更改为包含“”后,“Android”文本现在显示在Eclipse的xml图形布局视图中!尽管在第34行出现了“错误膨胀类com.example.overlay.DrawView”,它仍然会在模拟器中崩溃。在安卓系统中使用图形真的有这么难吗?内森和比杜尔夫。r:非常感谢,现在它真的很管用。
<com.example.overlay.DrawView
android:id="@+id/overlay"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY"/>
public DrawView(Context context, AttributeSet attrs) {
super(context, attrs);
}