我可以将editText添加到scrollview和任何其他组件,但是当我尝试插入绘图画布时,没有任何内容出现。我展示了代码以获取更多信息
package prueba.android.ondraw;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
public class Prueba_ondrawActivity extends Activity {
private static final String LOGTAG = "";
@Override
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
MyView v = new MyView(this);
layout.addView(v);
}
private class MyView extends View{
public MyView(Context context) {
super(context);
}
protected void onDraw(Canvas canvas) {
int j = 0;
for(int i=0;i<10;i++){
super.onDraw(canvas);
Paint paint = new Paint();
paint.setColor(Color.CYAN);
paint.setAntiAlias(true);
int ancho= canvas.getWidth();
String ancho2 = "" + ancho;
Log.e(LOGTAG,ancho2);
canvas.drawRect(0, 0,200,200, paint);
j= j+100;
}
}
}
}
main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="150dp"
android:layout_height="400dp"
android:layout_marginLeft="100dp"
android:background="#FFFFFF"
>
<LinearLayout
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</LinearLayout>
</ScrollView>
</LinearLayout>
我希望有人可以给我一个解决方案,因为它无法找到如何在scrollview中绘制
Thanksss
答案 0 :(得分:4)
您没有看到任何内容,因为您的自定义视图没有大小。
使用LayoutParams或直接传递像素大小。您还可以实施onMeasure
更多信息here
layout.addView(v,300,300);
此外,您不需要在每次迭代时调用super.onDraw或创建新的Paint。
答案 1 :(得分:0)
如果您只想绘制并显示它,那么您可以在ScrollView中放置一个ImageView并显示如下:
newBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
canvas = new Canvas(newBitmap);
canvas.drawSomething();
imageView.setImageBitmap(newBitmap);
希望这会有所帮助;)