【问题标题】:Android LAYOUT xml / java errorAndroid LAYOUT xml/java 错误
【发布时间】:2011-05-03 17:58:03
【问题描述】:

我正在尝试获得一个面板,其中包含一个包含图像的画布,我将把它放在另一个图像上,当我触摸屏幕时,顶部(覆盖)图像将通过 PoerterDuffXfermode(PorterDuff.Mode) 等被擦除,, 无论如何,感谢这个论坛上一个人的帮助,我已经完成了功能并除尘了,他提供了一些基本上完全符合我需要的代码,但是我有一个小问题,代码的实现,不允许我在 XML 中正确引用 Panel 类以将 Panel 放置在预定义的 XML (main.xml) 文件中。它给了我一个错误说明

自定义视图面板未使用 2 或 3 参数视图构造函数; XML 属性将不起作用

这就是我的 xml 在基本比例上的样子(只是外部线性布局中的视图)。

<com.easyscratch.full.Panel 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id ="@+id/easyCustView"
    android:layout_width="300dp"
    android:layout_height="300dp"
    android:visibility="visible"
    android:focusableInTouchMode="true"/>   

java如下.(PANEL CLASS)

package com.easyscratch.full;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Bitmap.Config;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

class Panel extends View
{

    private Bitmap  mBitmap;
    private Canvas  mCanvas;
    private Path    mPath;
    private Paint   mPaint;
    Bitmap bitmap;
    Canvas pcanvas ;
     int x = 0;
     int y =0;
     int r =0;
    public Panel(Context context) {
        super(context);

        Log.v("Panel", ">>>>>>");

        setFocusable(true);
        setBackgroundColor(Color.GREEN);

        // setting paint 
             mPaint = new Paint();
            mPaint.setAlpha(0);
            mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
            mPaint.setAntiAlias(true);

            // getting image from resources
            Resources r = this.getContext().getResources();

            Bitmap bm = BitmapFactory.decodeResource(getResources(),  R.drawable.foreground_image);

            // converting image bitmap into mutable bitmap

              bitmap =  Bitmap.createBitmap(295, 260, Config.ARGB_8888);

               pcanvas = new Canvas();
              pcanvas.setBitmap(bitmap);                   // drawXY will result on that Bitmap
              pcanvas.drawBitmap(bm, 0, 0, null);


    }

    @Override
    protected void onDraw(Canvas canvas) {


    // draw a circle that is  erasing bitmap            
        pcanvas.drawCircle(x, y, r, mPaint);

        canvas.drawBitmap(bitmap, 0, 0,null);

        super.onDraw(canvas);

    }



    @Override
    public boolean onTouchEvent(MotionEvent event) {

         // set paramete to draw circle on touch event
         x = (int) event.getX();
            y = (int) event.getY();

            r =20;
            // Atlast invalidate canvas
            invalidate();
            return true;
    }

}

基本主类调用 MAIN.XML

package com.easyscratch.full;

import android.app.Activity;
import android.os.Bundle;

public class easyscratch extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

,, 如果有人 cud 告诉我我做错了什么,或者可能是

的替代实现
public Panel(Context context) {
    super(context);

无论如何,非常感谢高级真的会尽快获得一些帮助:)

干杯!

【问题讨论】:

    标签: android xml layout view


    【解决方案1】:

    您的 Panel 构造函数至少还必须有一个 AttributeSet 字段。

    public Panel(Context context, AttributeSet attr){
       super.(context, attr);
    

    【讨论】:

    • 感谢您的回复,我已经添加了这一点,但现在它没有给我错误,它只是在我启动应用程序时崩溃。我使用 Galaxy Tab 真机在其 2.2 上测试我的应用。
    • 您可能必须启动 ddms 并发布 logcat 以查看导致崩溃的原因。
    • 没关系,我想通了 :) 非常感谢您的回复!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-04
    • 2019-02-23
    • 1970-01-01
    • 2011-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多