本文实例为大家分享了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>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。