J'essaie d'utiliser Canvas qui a été créé en classe externe. Toutefois, l'application ne s'exécute pas. Voici mon code pour MyImge où l'activité commenceen utilisant Canvas dans android
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
public class MyImage2 extends Activity {
DemoView draw;
private int imageSizeX = 2047;
private int imageSizeY = 2047;
private int current_drawable = R.drawable.image;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("blah", current_drawable);
ImageView img=(ImageView)findViewById(R.id.imageView);
Bitmap bmp=BitmapFactory.decodeResource(getResources() ,current_drawable);
draw = (DemoView)findViewById(R.id.demo);
imageSizeX = bmp.getWidth()/2;
imageSizeY = bmp.getHeight()/2;
Bitmap resizedbitmap=Bitmap.createScaledBitmap(bmp, imageSizeX, imageSizeY, true);
img.setImageBitmap(resizedbitmap);
}
}
et mon DemoView comme suit
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;
public class DemoView extends View{
public DemoView(Context context){
super(context);
}
@Override protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
// make the entire canvas white
paint.setColor(Color.WHITE);
canvas.drawPaint(paint);
paint.setAntiAlias(false);
paint.setColor(Color.GREEN);
canvas.drawRect(100, 5, 200, 30, paint);
canvas.drawLine(0, 300 , 320, 300, paint);
}
}
la mise en page
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="@+id/imageView"
android:layout_width="fill_parent"
android:scaleType="matrix"
android:layout_height="525px">
</ImageView>
<view class="com.test.DemoView"
android:id="@+id/demo"
android:layout_width="fill_parent"
android:layout_height="125px"/>
</LinearLayout>
si je retire l'étiquette com.myimage2.DemoView je peux voir l'image, mais mon but est de voir l'image et la toile. Quelqu'un pourrait-il aider s'il vous plaît.
journal d'erreur:
!ENTRY com.android.ide.eclipse.adt 2 0 2011-04-10 02:19:27.204
!MESSAGE AndroidManifest: Ignoring unknown 'com.test.DemoView' XML element
!ENTRY com.android.ide.eclipse.adt 2 0 2011-04-10 02:19:27.891
!MESSAGE AndroidManifest: Ignoring unknown 'view' XML element
Un grand merci à l'avance.
Essayez de nettoyer le projet. L'élément de vue doit être connu. – MByD