android实现拖拽裁剪功能
作者:hu5080126 时间:2022-03-10 09:37:14
本文实例为大家分享了android拖拽框,裁剪出图片的具体代码,供大家参考,具体内容如下
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import eebochina.com.testsomething.R;
public class DragTakePhoto extends AppCompatActivity implements View.OnTouchListener {
ImageView takePhoto;
ImageView takeImage;
Button mButton;
static final String tag = "takePhoto";
RelativeLayout.LayoutParams mLayoutParams;
int left, top, bottom, right;
int x, y;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drag_take_photo);
takePhoto = (ImageView) findViewById(R.id.take_layout);
takeImage = (ImageView) findViewById(R.id.take_image);
mButton = (Button) findViewById(R.id.take_crop);
takePhoto.setOnTouchListener(this);
takePhoto.post(new Runnable() {
@Override
public void run() {
Log.d(tag, "" + takePhoto.getX() + " ," + takePhoto.getY() + " :" + takePhoto.getPivotX() + " " + takePhoto.getPivotY());
}
});
mLayoutParams = (RelativeLayout.LayoutParams) takePhoto.getLayoutParams();
final Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.take);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
float wd = (bitmap.getWidth() * 10000 / takeImage.getWidth()) / 10000f;
float hd = (bitmap.getHeight() * 10000 / takeImage.getHeight()) / 10000f;
Bitmap bitmap1 = Bitmap.createBitmap(bitmap, (int) (takePhoto.getX() * wd), (int) (takePhoto.getY() * hd), (int) (takePhoto.getWidth() * wd), (int) (takePhoto.getHeight() * hd));
takePhoto.setImageBitmap(bitmap1);
}
});
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) takeImage.getLayoutParams();
left = layoutParams.leftMargin;
top = layoutParams.topMargin;
right = takeImage.getWidth() - left - takePhoto.getWidth();
bottom = takeImage.getHeight() - top - takePhoto.getHeight();
}
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
x = (int) motionEvent.getRawX() - mLayoutParams.leftMargin;
y = (int) motionEvent.getRawY() - mLayoutParams.topMargin;
break;
case MotionEvent.ACTION_MOVE:
int movex = (int) (motionEvent.getRawX() - x);
int movey = (int) (motionEvent.getRawY() - y);
Log.d(tag, "x:" + movex + " y:" + movey);
if (movex < left) movex = left;
if (movey < top) movey = top;
if (movex > right) movex = right;
if (movey > bottom) movey = bottom;
mLayoutParams.leftMargin = movex;
mLayoutParams.topMargin = movey;
takePhoto.setLayoutParams(mLayoutParams);
break;
}
takePhoto.invalidate();
return true;
}
}
布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="eebochina.com.testsomething.dragTake.DragTakePhoto">
<ImageView
android:id="@+id/take_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/take"
android:layout_marginBottom="50dp"
/>
<ImageView
android:id="@+id/take_layout"
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:background="@android:color/holo_blue_dark"
/>
<Button
android:id="@+id/take_crop"
android:layout_width="200dp"
android:layout_height="40dp"
android:background="@android:color/holo_blue_dark"
android:text="裁剪"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>
来源:https://blog.csdn.net/hu5080126/article/details/52515481
标签:android,拖拽,裁剪
0
投稿
猜你喜欢
基于C#实现磁性吸附窗体
2022-09-04 07:04:51
谷歌被屏蔽后如何搭建安卓环境
2022-10-23 02:07:36
通过自定制LogManager实现程序完全自定义的logger
2022-05-11 06:33:15
Java初学之继承与多态
2022-11-29 15:13:04
Java多线程常见案例分析线程池与单例模式及阻塞队列
2022-06-01 06:42:42
android中实现在ImageView上随意画线涂鸦的方法
2023-03-04 07:55:23
MyBatis-Plus联表查询(Mybatis-Plus-Join)的功能实现
2023-11-25 03:24:33
mvc开启gzip压缩示例分享
2022-05-03 08:34:29
proguar在Android混淆中的用法
2021-10-01 01:12:12
spring springMVC中常用注解解析
2023-09-14 20:45:46
java 8如何自定义收集器(collector)详解
2022-02-12 07:22:17
C#键值对容器的介绍
2023-04-14 12:26:56
Java实现简单台球游戏
2022-06-28 23:55:59
SpringMVC Restful api接口实现的代码
2023-11-29 12:34:27
Java 中的位运算与移位运算详解
2022-03-10 09:34:19
SpringCloud入门实验环境搭建
2022-11-17 10:21:14
java读取resources文件详解及实现代码
2022-07-15 15:11:30
Android实现双击返回键退出应用实现方法详解
2023-03-23 21:33:29
c#消息提示框messagebox的详解及使用
2022-06-25 14:57:44
Java基于Socket实现多人聊天室
2022-11-08 14:11:12