1

我和我的朋友正在做一个更大的 Android 项目。这件作品需要做的是允许我们使用位图和表面视图在触摸屏上实现手绘。需要明确的是,我们最终想要的是当用户实例化一个触摸事件时,适当的像素会改变颜色,例如,如果用户在触摸屏上写下他们的名字,他们的名字就会出现在他们自己的笔迹中。我相信问题出在 invalidate 方法中,该方法在触摸屏幕时调用。使用日志输出,我们发现未调用/使用 onDraw 方法。我们使用位图的原因是因为它们可以被压缩并通过网络发送。如果有更好的方法来实现该功能,那么我们会接受建议。

`/**
 * 
 */
package com.example.practice_app;
import android.util.DisplayMetrics;
import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.SurfaceView;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.SurfaceHolder;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Color;

//Debugging packages
import android.util.Log;
import java.lang.RuntimeException;

/**
 * @author Asus
 *
 */
public class WhiteBoardView extends SurfaceView implements SurfaceHolder.Callback {


    //Class fields:
    //SurfaceHolder:
    SurfaceHolder holder;

    //Current context:
    Context currentContext;

    //Graphics:
    private DisplayMetrics metrics = new DisplayMetrics();

    //private Bitmap ourBitmap;
    private Bitmap ourBitmap;
    private Canvas ourCanvas;
    private Paint paint;
    private View myView;




    //Class Methods:
    /**
     * @param context
     */

    public WhiteBoardView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        final String CONSTRU_TAG = "Constructor";

        //Initialize class fields;
        currentContext = context;
        holder = getHolder();

        holder.addCallback(this);

        //Create the Canvas and the Bitmap:
         metrics = new DisplayMetrics();
        ((WindowManager)getWindowManager()).getDefaultDisplay().getMetrics(metrics);

        try{ourBitmap = Bitmap.createBitmap(metrics.widthPixels, metrics.heightPixels, Bitmap.Config.ARGB_8888);}
        catch(IllegalArgumentException argEr){
            Log.e("BITMAP_CREATE","Error creating the bitmap: IllegalArgumentException");
        }


        ourCanvas = new Canvas(ourBitmap);
        if(ourBitmap == null)
        {
            Log.e(CONSTRU_TAG, "ourBitmap is \"null\".");
        }
        paint = new Paint();
        paint.setColor(Color.WHITE);


    }

    private Object getWindowManager() {
        // TODO Auto-generated method stub
        return currentContext.getSystemService(Context.WINDOW_SERVICE);
    }

    /**
     * @param context
     * @param attrs
     */
    public WhiteBoardView(Context context, AttributeSet attrs) {
        super(context, attrs);

        // TODO Auto-generated constructor stub
        final String CONSTRUC_TAG2 = "Constructor(Context, AttributeSet)";
        // TODO Auto-generated constructor stub

        //Initialize class fields;
        currentContext = context;
        holder = getHolder();

        holder.addCallback(this);

        //Create the Canvas and the Bitmap:
         metrics = new DisplayMetrics();
        ((WindowManager)getWindowManager()).getDefaultDisplay().getMetrics(metrics);

        try{ourBitmap = Bitmap.createBitmap(metrics.widthPixels, metrics.heightPixels, Bitmap.Config.ARGB_8888);}
        catch(IllegalArgumentException argEr){
            Log.e("BITMAP_CREATE","Error creating the bitmap: IllegalArgumentException");
        }


        ourCanvas = new Canvas(ourBitmap);
        if(ourBitmap == null)
        {
            Log.e(CONSTRUC_TAG2, "ourBitmap is \"null\".");
        }
        paint = new Paint();
        paint.setColor(Color.WHITE);
    }

    /**
     * @param context
     * @param attrs
     * @param defStyle
     */
    public WhiteBoardView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
        final String CONSTRUC_TAG3 = "Constructr(Context, AttributeSet, int)";
        // TODO Auto-generated constructor stub

        //Initialize class fields;
        currentContext = context;
        holder = getHolder();

        holder.addCallback(this);

        //Create the Canvas and the Bitmap:
         metrics = new DisplayMetrics();
        ((WindowManager)getWindowManager()).getDefaultDisplay().getMetrics(metrics);

        try{ourBitmap = Bitmap.createBitmap(metrics.widthPixels, metrics.heightPixels, Bitmap.Config.ARGB_8888);}
        catch(IllegalArgumentException argEr){
            Log.e("BITMAP_CREATE","Error creating the bitmap: IllegalArgumentException");
        }


        ourCanvas = new Canvas(ourBitmap);
        if(ourBitmap == null)
        {
            Log.e(CONSTRUC_TAG3, "ourBitmap is \"null\".");
        }
        paint = new Paint();
        paint.setColor(Color.RED);
    }

    //Event Handlers:
    @SuppressLint("WrongCall")
    @Override
    public boolean onTouchEvent(MotionEvent e)
    {
        //Declare local variables:
        final String ONTOUCH_TAG = "onTouchEvent(MotionEvent)";
        boolean eventHandled = true;
        float x = e.getX();
        float y = e.getY();

        switch(e.getAction())
        {

        case MotionEvent.ACTION_DOWN: //Log.i(ONTOUCH_TAG, "Hurray! Touch is working!");
        case MotionEvent.ACTION_MOVE:
        case MotionEvent.ACTION_UP:
            if(((x >= 0) && (x <= ourBitmap.getWidth())) && ((y >= 0) && (y <= ourBitmap.getHeight())))
            {  

               ourBitmap.setPixel((int)x, (int)y, paint.getColor());

                   invalidate();

            }

            break;

    }
        return eventHandled;
    }

    @Override
    public void onDraw(Canvas canvas)
    {
        canvas.drawBitmap(ourBitmap, null, paint);
    }

    @Override
    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
        // TODO Auto-generated method stub

    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        // TODO Auto-generated method stub

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub

    }
}'

And here is the Activity that WhiteBoardView is associated with.

package com.example.practice_app;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Menu;

public class Whiteboard extends Activity {

    //Class fields:
    Bitmap ourBitmap;
    Canvas ourCanvas;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //Call the superclass constructor:
        super.onCreate(savedInstanceState);

        //Get the current Device's measurements:
        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);

        //Create the Bitmap and Canvas objects
        ourBitmap = Bitmap.createBitmap(metrics.widthPixels, metrics.heightPixels, Bitmap.Config.ARGB_8888);
        ourCanvas = new Canvas(ourBitmap);

        //Set the UI layout for this activity:
        setContentView(R.layout.activity_whiteboard);

    }
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.whiteboard, menu);
        return true;
    }

}
4

1 回答 1

0

当您在 SurfaceView 上绘图时,您需要使用这些surfaceCreated / Updated / Destroyed方法并SurfaceSurfaceHolder.

如果你改变你的类来扩展View和实现onMeasure它应该可以工作。

于 2013-03-23T04:31:31.183 回答