android實(shí)現(xiàn)拖拽裁剪功能
本文實(shí)例為大家分享了android拖拽框,裁剪出圖片的具體代碼,供大家參考,具體內(nèi)容如下
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>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android實(shí)現(xiàn)拍照、選擇圖片并裁剪圖片功能
- Android裁剪圖片為圓形圖片的實(shí)現(xiàn)原理與代碼
- 解決Android從相冊(cè)中獲取圖片出錯(cuò)圖片卻無(wú)法裁剪問(wèn)題的方法
- Android實(shí)現(xiàn)從本地圖庫(kù)/相機(jī)拍照后裁剪圖片并設(shè)置頭像
- Android 7.0中拍照和圖片裁剪適配的問(wèn)題詳解
- Android自定義View實(shí)現(xiàn)照片裁剪框與照片裁剪功能
- Android實(shí)現(xiàn)拍照及圖片裁剪(6.0以上權(quán)限處理及7.0以上文件管理)
- Android編程實(shí)現(xiàn)調(diào)用系統(tǒng)圖庫(kù)與裁剪圖片功能
- Android拍照或從圖庫(kù)選擇圖片并裁剪
- Android圖片裁剪功能實(shí)現(xiàn)代碼
相關(guān)文章
神奇的listView實(shí)現(xiàn)自動(dòng)顯示隱藏布局Android代碼
這篇文章主要介紹了神奇的listView實(shí)現(xiàn)自動(dòng)顯示隱藏布局Android代碼實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09Android自定義view實(shí)現(xiàn)水波進(jìn)度條控件
這篇文章主要為大家詳細(xì)介紹了Android自定義view實(shí)現(xiàn)水波進(jìn)度條控件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05Android TextView Marquee的應(yīng)用實(shí)例詳解
這篇文章主要介紹了Android TextView Marquee的應(yīng)用實(shí)例詳解的相關(guān)資料,這里說(shuō)明使用方法及簡(jiǎn)單實(shí)例和注意實(shí)現(xiàn),需要的朋友可以參考下2017-08-08Android編程實(shí)現(xiàn)設(shè)置按鈕背景透明與半透明及圖片背景透明的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)設(shè)置按鈕背景透明與半透明及圖片背景透明的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Button及ImageButton的背景屬性設(shè)置技巧,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2015-12-12android+json+php+mysql實(shí)現(xiàn)用戶反饋功能方法解析
相信每個(gè)項(xiàng)目都會(huì)有用戶反饋建議等功能,這個(gè)實(shí)現(xiàn)的方法很多,下面是我實(shí)現(xiàn)的方法,供大家交流2012-11-11Android截取視頻幀并轉(zhuǎn)化為Bitmap示例
利用MediaMetadataRetriever按照時(shí)間截取視頻并轉(zhuǎn)換為Bitmap存放于SDCard,具體實(shí)現(xiàn)如下,感興趣的朋友可以參考下哈2013-06-06android端微信支付V3版本地簽名統(tǒng)一下單詳解
本篇文章主要介紹了android端微信支付V3版本地簽名統(tǒng)一下單,具有一定的參考價(jià)值,有興趣的同學(xué)可以了解一下。2016-11-11Android開(kāi)發(fā)中ImageLoder進(jìn)行圖片加載和緩存
這篇文章主要介紹了Android開(kāi)發(fā)中ImageLoder進(jìn)行圖片加載和緩存的相關(guān)資料,需要的朋友可以參考下2016-04-04kotlin anko頁(yè)面跳轉(zhuǎn)實(shí)現(xiàn)方式,攜帶參數(shù)或flag
這篇文章主要介紹了kotlin anko頁(yè)面跳轉(zhuǎn)實(shí)現(xiàn)方式,攜帶參數(shù)或flag,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03