2

嗨,我是 android 的新手,也是 Stackoverflow 的新手。我想创建简单的裁剪应用程序,所以我使用图像视图并在 this.when 我触摸它时应用 on touch 监听器。我画的简单又小默认rect。之后,当我在屏幕上拖动光标时,我想增加rect运行时的大小。所以在方法上应用属性数组。onDraw但我做不到。我的代码如下。

我的绘图视图类

public class DrawView extends View 
{   

Paint paint ;

Canvas myCanvas;

float []atr;

public DrawView(Context context) 
{
    super(context);

}

public DrawView(Context context, float [] atr) 
{
      super(context);
      this.atr=atr;
}
@Override
protected void onDraw(Canvas canvas) 
{
    Paint paint = new Paint();
    paint.setStyle(Paint.Style.STROKE);     
    paint.setColor(Color.BLUE);     
    canvas.drawRect(atr[0], atr[1], atr[2],atr[3], paint);
    //System.err.println("After Call on Draw Method");      
}
}

我在这个类中使用我的父类。

enter code hereContext myContext;
RelativeLayout relMainLayout,relHeaderLayout,relBelowLayout;    

DrawView drawView;

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);

    myContext=DrawRectActivity.this;

    LayoutInflater layoutInflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);   

    int layoutId = myContext.getResources().getIdentifier("main","layout",getPackageName());

    relMainLayout = (RelativeLayout) layoutInflater.inflate(layoutId,null);

    relHeaderLayout=(RelativeLayout) relMainLayout.findViewById(R.id.relHeaderLayout);

    relBelowLayout=(RelativeLayout) relMainLayout.findViewById(R.id.relBelowLayout);

    /*int[] atr={10,10,00,00};
    drawView=new DrawView(myContext,atr);
    drawView.bringToFront();
    drawView.setBackgroundColor(Color.YELLOW);
    RelativeLayout.LayoutParams drawParams=new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,200);
    drawParams.addRule(RelativeLayout.BELOW, relHeaderLayout.getId());      
    relMainLayout.addView(drawView, drawParams);    */

    setContentView(relMainLayout);
   //drawView.setOnTouchListener(this);
    //relMainLayout.setOnTouchListener(this);
}

public boolean onTouch(View v, MotionEvent event) 
{
    float x,y;
    float incx=5,incy=5;
    x=event.getX();
    y=event.getY();     

    switch (event.getActionMasked()) 
    {       
    case  MotionEvent.ACTION_DOWN:

        System.err.println("X-->"+x);

        System.err.println("Y-->"+y);

        float[] atr={x,y,x+incx,y+incy};
        drawView=new DrawView(myContext,atr);
        drawView.bringToFront();
        //drawView.setBackgroundColor(Color.YELLOW);
        RelativeLayout.LayoutParams drawParams=new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,200);
        drawParams.addRule(RelativeLayout.BELOW, relHeaderLayout.getId());      
        relMainLayout.addView(drawView, drawParams);
        break;

    case MotionEvent.ACTION_MOVE:
        float x1=event.getX();
        float y1=event.getY();
        incx=x1+incx;
        incy=y1+incy;
        float[] at1={x,y,incx,incy};
        drawView=new DrawView(myContext,at1);
        drawView.bringToFront();            
        break;

    case MotionEvent.ACTION_UP:

        System.err.println("Up X-->"+x);
        System.err.println("Up y-->"+y);

        System.err.println("Up X-->"+incx);
        System.err.println("Up y-->"+incy);


        break;

    default:

        break;
    }



    /*int[] atr={100,100,00,00};        
    DrawView drawView2=new DrawView(myContext, atr);
    Bitmap myCanvasBitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
    Canvas myCanvas = new Canvas();
    myCanvas.setBitmap(myCanvasBitmap); 
    //canvas.setDensity(BIND_AUTO_CREATE);      
    drawView2.draw(myCanvas);       */  
    return false;
}

并在此使用触摸事件。

4

0 回答 0