使用Finger Paints如何激活比较功能?

时间:2012-04-23 14:42:19

标签: android canvas drawing

我尝试在Finger Paints应用程序中比较2位图。我首先将第一个位图加载到2d数组中,然后我想将其他输出位图从finger paint加载到另一个数组中并比较两个位图。我的代码不起作用,我不知道我应该在哪里编辑。比较功能在用户按下选项菜单中的“比较”选项后立即开始。请帮帮我。

public class MainActivity extends GraphicsActivity
    {

boolean check = false;  


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(new MyView(this));

    mPaint = new Paint();                                                                       
    mPaint.setAntiAlias(true);                                          
    mPaint.setDither(true);                                             
    mPaint.setColor(Color.RED);                                         
    mPaint.setStyle(Paint.Style.STROKE);                                
    mPaint.setStrokeJoin(Paint.Join.ROUND);                             
    mPaint.setStrokeCap(Paint.Cap.ROUND);                               
    mPaint.setStrokeWidth(20);
}

private Paint   mPaint;

public class MyView extends View {
    Bitmap mBitmap;
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.c_1);
    Bitmap bitmap2;
    private Canvas  mCanvas;
    private Path    mPath;
    private Paint   mBitmapPaint;

    int w1 = bitmap.getWidth();
    int h1 = bitmap.getHeight();

    int [][] array = new int [w1][h1];
    int b=0;
    int c=0;
    int k=0;
    int i, j;
    int l=0;
    View content = this;


    {
        for(int j = 0;j< w1; j++){
            for(int i = 0;i< h1; i++,k++){
                array[j][i]= bitmap.getPixel(j, i);
                if(bitmap.getPixel(j, i)== 100);
                {
                    b++;
                }

            }
        }
        System.out.println("numbers of gray:" + b);       

    }


    public MyView(Context c) {                                              
        super(c);

        mPath = new Path();
        mBitmapPaint = new Paint(Paint.DITHER_FLAG);
        content.setDrawingCacheEnabled(true);
        content.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);

}

    @Override
    public void onSizeChanged(int w, int h, int oldw, int oldh){
        super.onSizeChanged(w, h, oldw, oldh);
        mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(mBitmap);
    }


    @Override
    protected void onDraw(Canvas canvas) {

        canvas.drawColor(Color.WHITE);

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

        canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);

        canvas.drawPath(mPath, mPaint);

        bitmap2 = content.getDrawingCache();
    }




    private float mX, mY;
    private static final float TOUCH_TOLERANCE = 4;

    private void touch_start(float x, float y) {
            mPath.reset();
            mPath.moveTo(x, y);
            mX = x;
            mY = y;

        }
    private void touch_move(float x, float y) {
        float dx = Math.abs(x - mX);
        float dy = Math.abs(y - mY);
        if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
            mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
            mX = x;
            mY = y;
            }
    }
    private void touch_up() {
        mPath.lineTo(mX, mY);
        mCanvas.drawPath(mPath, mPaint);
        mPath.reset();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float x = event.getX();
        float y = event.getY();         

        System.out.println("Coordinates:" + x + "," + y);

    switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                touch_start(x, y);
                invalidate();
                break;
            case MotionEvent.ACTION_MOVE:
                touch_move(x, y);
                invalidate();
                break;
            case MotionEvent.ACTION_UP:
                touch_up();
                invalidate();
                break;
    }                               

    return true;    

    }  

    private void compare(Boolean check){
    if(check == true)
        {

            int w2 = bitmap2.getWidth();
            int h2 = bitmap2.getHeight();
            System.out.println("Width and Height of the product:" + w2 + "," + h2); 

            int [][] array1 = new int [w2][h2];             
            {               
                for(int j1 = 0;j1< w2; j1++){
                    for(int i1 = 0;i1< h2; i1++,l++){
                        array1[j1][i1]= bitmap2.getPixel(j1, i1);
                        if(array[j1][i1]== 100 && array1[j1][i1] == Color.RED);
                        {
                            c++;
                        }

                    }
                }
                System.out.println("numbers pixels touched:" + c);       
            }
            int d=(b-c)/b*100;

            Toast t = Toast.makeText(getBaseContext(), "Completion :" + d + "%", Toast.LENGTH_SHORT);
            t.show();

        }
    }
}

private static final int CLEAR_MENU_ID = Menu.FIRST;
private static final int COMPARE_ID = Menu.FIRST + 1;

public boolean onCreateOptionsMenu(Menu menu)  {
    super.onCreateOptionsMenu(menu);

    menu.add(0, CLEAR_MENU_ID, 0, "Clear ALL");
    menu.add(0, COMPARE_ID, 0, "Compare");

 return true;
   }

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
        case CLEAR_MENU_ID:
            setContentView(new MyView(this));
            check = false;
            return true;
        case COMPARE_ID:
            check = true;
            return true;
    }
            return super.onOptionsItemSelected(item);   
    }
}

1 个答案:

答案 0 :(得分:0)

您并未在任何地方调用compare()。

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
        case CLEAR_MENU_ID:
            setContentView(new MyView(this));
            check = false;
            return true;
        case COMPARE_ID:
            check = true;
            compare(check);
            return true;
        default:
            break;
    }

    return super.onOptionsItemSelected(item);   

}

我无法看到&#34;检查&#34;的目的。变量,但我假设你在其他地方使用它。