【发布时间】:2012-08-04 03:55:44
【问题描述】:
嗨,我想创建简单的cropImage Apps.so 我使用canvas 并绘制它 rubtime。我在视图上绘制画布。所以我想剪辑画布覆盖的区域。
所以我喜欢这个。
首先创建Canvas Example
public class CanvasExample extends Activity
{
/** Called when the activity is first created. */
RelativeLayout relMainOperationLayout;
RelativeLayout relTabHeader;
RelativeLayout relMidalLayout;
RelativeLayout relBelowLayout;
Context myContext;
DrawCanvas drawCanvas;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
myContext=CanvasExample.this;
LayoutInflater layoutInflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int layoutId = myContext.getResources().getIdentifier("main","layout",getPackageName());
relMainOperationLayout = (RelativeLayout) layoutInflater.inflate(layoutId,null);
relTabHeader=(RelativeLayout) relMainOperationLayout.findViewById(R.id.relHeadLayout);
relMidalLayout=(RelativeLayout) relMainOperationLayout.findViewById(R.id.relmidalLayout);
relBelowLayout=(RelativeLayout) relMainOperationLayout.findViewById(R.id.relBelowLayout);
drawCanvas=new DrawCanvas(CanvasExample.this,myContext);
//drawCanvas.setBackgroundColor(Color.YELLOW);
drawCanvas.setBackgroundDrawable(CanvasExample.this.getResources().getDrawable(R.drawable.ic_launcher));
RelativeLayout.LayoutParams drawParams=new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,200);
drawParams.addRule(RelativeLayout.BELOW, relTabHeader.getId());
//relMidalLayout.addView(drawCanvas,drawParams);
relMainOperationLayout.addView(drawCanvas,drawParams);
setContentView(relMainOperationLayout);
}
在此之后我创建一个类DrawCanvas 扩展View。
并绘制Canvas 运行时间。
public class DrawCanvas extends View
{
Context drawContext;
Activity drawActivity;
ImageView image;
Paint mPaint;
int left=0,right=0,top=0,bottom=0;
Canvas passCanvas;
public DrawCanvas(Activity activity,Context context)
{
super(activity);
this.drawActivity=activity;
this.drawContext=context;
mPaint = new Paint();
mPaint.setDither(true);
mPaint.setColor(Color.RED);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(3);
this.setOnTouchListener(new View.OnTouchListener() {
//@Override
public boolean onTouch(View v, MotionEvent event)
{
switch (event.getActionMasked())
{
case MotionEvent.ACTION_DOWN:
invalidate();
left=(int) event.getX();
right=left+1;
top=(int) event.getY();
bottom=top+1;
mPaint.setColor(Color.BLACK);
onDraw(passCanvas=new Canvas());
break;
case MotionEvent.ACTION_MOVE:
invalidate();
int tempX=(int) event.getX();
if(tempX>right)
{
right=right+1;
}else
{
right=right-1;
}
int tempY=(int) event.getY();
if(tempY>bottom)
{
bottom=bottom+1;
}else
{
bottom=bottom-1;
}
mPaint.setColor(Color.GREEN);
onDraw(passCanvas=new Canvas());
break;
case MotionEvent.ACTION_UP:
invalidate();
mPaint.setColor(Color.RED);
onDraw(passCanvas=new Canvas());
System.err.println("After Touch Up");
Rect rect= passCanvas.getClipBounds();
System.err.println("Save Canvas-->"+passCanvas.save());
System.err.println("Display Rect Width==>"+rect.toString());
break;
default:
break;
}
return true;
}
});
}
@Override
protected void onDraw(Canvas canvas)
{
canvas.drawRect(left, top, right, bottom, mPaint);
}
}
所以现在我要做什么,我想用画布裁剪覆盖的区域。 提前致谢。
当我 getClipBounds 它现在显示空值我该怎么做。
【问题讨论】:
-
调用 onDraw(passCanvas=new Canvas());从 onTouch(View v, MotionEvent event) 不会改变 UI,你必须调用 invalidate();然后android将在下一个ui循环中调用onDraw。
-
我做所有事情。我的问题是如何获得 Canvas 覆盖的区域
-
getWidth(), getHeight() 将为您提供视图的宽度和高度。
-
我这样做但它
s doesnt 帮助我。在 android 2.2 中选择和裁剪图像的任何其他方式