问题描述
您好我想创建简单的cropImage Apps.so我使用画布
并提请其rubtime.i画在画布上我希望得到裁剪的区域涵盖了view.so通过画布。所以在这里,我不喜欢这样。
hi i want create simple cropImage Apps.so I Use canvas
and draw it rubtime.i draw the canvas over the view.so i want get clip the area which cover by canvas.so here i do like this.
首先创建画布示例
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
扩展查看
。并绘制画布
运行时间。
after this i create one class DrawCanvas
extends View
.and Draw the Canvas
run time.
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);
}
}
所以现在我做什么,我想通过画布裁剪覆盖面积。预先感谢。
so now i what to do i want crop the covered area by canvas.Thank in advance.
当我getClipBounds它显示为空值,现在我做的事情。
when i getClipBounds it Display null value now i what to Do.
推荐答案
我想你想要做非矩形裁剪。请参阅Android - 裁剪图像的多点或How作物在Android上四个点之间的图像
I think you want to do non rectangular cropping. Please refer to Android - Crop an image from multipoints or How to crop an image in between four points on Android
您可能会发现它们非常有用。
You might find them useful.
最好的编码!
这篇关于如何让选择该区域在机器人帆布覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!