ImageView demo之美女抠图,有误差

ImageView 之美女抠图,有误差


现在做一个美女抠图demo,不过是有误差的,本人菜鸟,不懂高级算法,只能根据现在所学的写demo,大家见谅。。。


Xml代码如下,

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="subAlpha"
            android:text="-" />

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="addAlpha"
            android:text="+" />

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="prePage"
            android:text="<" />

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="nextPage"
            android:text=">" />
    </LinearLayout>

    <ImageView
        android:layout_width="500px"
        android:layout_height="600px"
        android:src="@drawable/s1"
        android:id="@+id/iv_two_image"
        android:background="#00ff00"
        android:scaleType="fitStart"
        />
    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="#ff0000"
        android:id="@+id/iv_two_new"
        />

</LinearLayout>

java代码如下:

package com.zking.g150715_android07_imageview;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.File;

/**
 * Created by Administrator on 2016/9/6.
 */
public class TwoActivity extends AppCompatActivity {

    private ImageView iv_two_image;
    private int currentAlpha=255;

    //定义图片资源
    private int images[]={R.drawable.s1,R.drawable.s2,R.drawable.s3,R.drawable.s4,R.drawable.s5};
    private int currentPage=0;
    private File[] files;
    private Bitmap bitmap;
    private ImageView iv_two_new;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_two);
        iv_two_image = (ImageView) findViewById(R.id.iv_two_image);
        iv_two_new = (ImageView) findViewById(R.id.iv_two_new);

        //01.判断手机是否有无内存卡,并且可用
        if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
            //得到内存卡的根目录
            String sdCardPath=Environment.getExternalStorageDirectory().getAbsolutePath();

            File file=new File(sdCardPath+"/images");
            files = file.listFiles();
        }
        showImage(currentPage);


                //给ImageView设置触摸事件
                iv_two_image.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {

                //求出缩放比
                //图片的高/控件的高
                float scaleBi=(float)iv_two_image.getHeight()/bitmap.getHeight();
                Log.i("test","scaleBi="+scaleBi);

                //得到触摸的坐标(实际触摸的是控件)
                float x=motionEvent.getX();
                float y=motionEvent.getY();
                Log.i("test","x="+x+" "+bitmap.getWidth());
                //求出缩放之后的坐标(图片的坐标)
                x=x/scaleBi;
                y=y/scaleBi;
                //x 图片
                if(x>bitmap.getWidth()||y>bitmap.getHeight()){
                    Toast.makeText(TwoActivity.this, "超出范围了", Toast.LENGTH_SHORT).show();
                }else{
                    if(x+50>bitmap.getWidth()){
                        x-=50;
                    }
                    if(y+50>bitmap.getHeight()){
                        y-=50;
                    }
                    //抠图
                    Bitmap newBitmap=Bitmap.createBitmap(bitmap,(int)x,(int)y,50,50);
                    iv_two_new.setImageBitmap(newBitmap);
                }
                return true;
            }
        });

    }

    /**
     * 减小透明度
     * @param view
     */
    public void subAlpha(View view){
        currentAlpha-=20;
        if(currentAlpha<=0){
            currentAlpha=0;
        }
        iv_two_image.setImageAlpha(currentAlpha);
    }

    /**
     * 增加透明度
     * @param view
     */
    public void addAlpha(View view){
        currentAlpha+=20;
        if(currentAlpha>=255){
            currentAlpha=255;
        }
        iv_two_image.setImageAlpha(currentAlpha);
    }

    /**
     * 上一张
     * @param view
     */
    public void prePage(View view){
        currentPage--;
        if(currentPage<0){
            currentPage=0;
            Toast.makeText(TwoActivity.this, "哥,不要在↑戳了", Toast.LENGTH_SHORT).show();
        }
        showImage(currentPage);
    }

    /**
     * 下一张
     * @param view
     */
    public void nextPage(View view){
        currentPage++;
        if(currentPage>images.length-1){
            currentPage=images.length-1;
            Toast.makeText(TwoActivity.this, "哥,不要在↓戳了", Toast.LENGTH_SHORT).show();
        }
        showImage(currentPage);
    }

    /**
     * 设置图片
     * @param currentPage
     */
    public void showImage(int currentPage){
            // iv_two_image.setImageResource(images[currentPage]);

            //展示内存卡中的图片
            //将File类型的图片转换成Bitmap类型
            bitmap = BitmapFactory.decodeFile(files[currentPage].getAbsolutePath());
            iv_two_image.setImageBitmap(bitmap);
    }
}

这个便是美女抠图的demo




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值