[英]Cropping image using createbitmap and a view
I have a view that can be dragged anywhere on the screen, so if a user drags it on the screen. 我有一个视图,可以将其拖到屏幕上的任何位置,因此如果用户将其拖到屏幕上。 Then everything within the view will only be cropped, how would I be able to achieve that?
然后,视图中的所有内容都将被裁剪掉,我将如何实现呢?
I have been able to crop by just calculating which part to crop, but what if I want to crop anything that is within the bounds of the View? 我只需要计算要裁剪的部分就可以裁剪,但是如果要裁剪视图范围内的任何东西怎么办?
This is my Actiivty: 这是我的活动:
View views;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnCrop = (Button) findViewById(R.id.btnCrop);
imageView = (ImageView) findViewById(R.id.imageView);
views = (View) findViewById(R.id.view);
views.setOnTouchListener(new View.OnTouchListener() {
private int initialX;
private int initialY;
private float initialTouchX;
private float initialTouchY;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
initialX = (int) views.getX();
initialY = (int) views.getY();
initialTouchX = event.getRawX();
initialTouchY = event.getRawY();
return true;
case MotionEvent.ACTION_MOVE:
views.setX(initialX + (int) (event.getRawX() - initialTouchX));
views.setY(initialY + (int) (event.getRawY() - initialTouchY));
return true;
}
return false;
}
});
btnCrop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bitmap croppedBmp = Bitmap.createBitmap(mbitmap, 0, 0,
mbitmap.getWidth(), mbitmap.getHeight() / 2);
imageView.setImageBitmap(croppedBmp);
}
});
}
public Bitmap getBitmapOFRootView(View v) {
View screenView = v.getRootView();
screenView.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(screenView.getDrawingCache());
screenView.setDrawingCacheEnabled(false);
return bitmap;
}
Thanks in advance for any help and insight on what can be done! 在此先感谢您提供的任何帮助和见解! :D
:D
You don't actually have to reinvent the wheel, there are available libraries for this such as https://github.com/ArthurHub/Android-Image-Cropper and https://github.com/Yalantis/uCrop . 您实际上不必重新发明轮子,有一些可用的库,例如https://github.com/ArthurHub/Android-Image-Cropper和https://github.com/Yalantis/uCrop 。
Though, if you would like to study how they did it the codes there are opensource. 但是,如果您想研究他们是如何做到的,那么代码是开源的。 Check them out.
去看一下。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.