Reputation:
I can add a editText to the scrollview and any other component but when I try to insert a drawing canvas, nothing appears. I show the code for more information
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>
I hope that someone can give me a solution because it can not find how to draw within the scrollview
Thanksss
Upvotes: 1
Views: 4658
Reputation: 6929
You are seeing nothing because your custom view has no size.
Either use LayoutParams or pass in the pixel size directly. You could also implement onMeasure
Some more info is here
layout.addView(v,300,300);
Also you don't need to call super.onDraw or create a new Paint on every iteration.
Upvotes: 5
Reputation: 421
If you just want to draw and show it then you can put an ImageView in the ScrollView and display it like this:
newBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
canvas = new Canvas(newBitmap);
canvas.drawSomething();
imageView.setImageBitmap(newBitmap);
hope this helps ;)
Upvotes: 0