android listview仿ios 3dTouch效果

最近項目上要求在listview上實現類似ios 3dTouch功能,現在網上搜索一番,發現該文章很好的實現了這個功能,於是在這文章的基礎上我自己做了一個改進。效果如圖所示:
                                 
實現思路:

1.截取圖片

 // 獲取屏幕照片
    public static Bitmap captureScreen(Activity activity) {

        activity.getWindow().getDecorView().setDrawingCacheEnabled(true);

        Bitmap bmp = activity.getWindow().getDecorView().getDrawingCache();

        return bmp;

    }

2.做高斯模糊處理

 // 高斯模糊處理
    private Bitmap blur(Bitmap bitmap, float radius) {
        Bitmap output = Bitmap.createBitmap(bitmap); // 創建輸出圖片
        RenderScript rs = RenderScript.create(this); // 構建一個RenderScript對象
        ScriptIntrinsicBlur gaussianBlue = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs)); //
        // 創建高斯模糊腳本
        Allocation allIn = Allocation.createFromBitmap(rs, bitmap); // 開闢輸入內存
        Allocation allOut = Allocation.createFromBitmap(rs, output); // 開闢輸出內存
        gaussianBlue.setRadius(radius); // 設置模糊半徑,範圍0f<radius<=25f
        gaussianBlue.setInput(allIn); // 設置輸入內存
        gaussianBlue.forEach(allOut); // 模糊編碼,並將內存填入輸出內存
        allOut.copyTo(output); // 將輸出內存編碼爲Bitmap,圖片大小必須注意
        rs.destroy(); // 關閉RenderScript對象,API>=23則使用rs.releaseAllContexts()
        return output;
    }
3.按壓實現動態高斯模糊
思路:佈局的時候下面一張高斯模糊的圖片,上面高斯模糊的圖片,對上面的圖片進行動態透明度變化,這時候看效果就是動態高斯模糊了
4.在按壓位置出現一個新的控件浮在上面
思路:動態添加view,通過計算點擊位置獲取listview的item位置,給該view設置setMargins控制其位置
5.優化
代碼:

xml文件

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/root"
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

    <ListView
        android:id="@+id/test_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </ListView>

    <ImageView
        android:id="@+id/image_down"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

    <ImageView
        android:id="@+id/image_up"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
     <TextView
    android:id="@+id/item_tv_ss"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:textSize="30sp"
    android:background="#ffffff"
    />
</RelativeLayout>

Activity文件
package memorandum.ios.csc.com.memorandum.activity;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.renderscript.Allocation;
import android.renderscript.Element;
import android.renderscript.RenderScript;
import android.renderscript.ScriptIntrinsicBlur;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import memorandum.ios.csc.com.memorandum.R;

/**
 * Created by csc on 17-7-31.
 */
public class TestActivity extends Activity{
    private ListView testListView;
    private List<Map<String, String>> data;
    private ImageView imageUp, imageDown;
    private Bitmap sampleImg;
    private Bitmap gaussianBlurImg;
    private int count = 0;
    private View attachedView;
    private RelativeLayout rootRl;
    private TextView testTv;
    RelativeLayout.LayoutParams lp;
    private boolean is3DTouch = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //取消標題欄
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //取消狀態欄
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.test);
        initData();

        imageUp = (ImageView) findViewById(R.id.image_up);
        imageDown = (ImageView) findViewById(R.id.image_down);


        testListView = (ListView) findViewById(R.id.test_list);
        testListView.setAdapter(new SimpleAdapter(this, data, R.layout.listview_item, new String[]{"name"},
                new int[]{R.id.item_tv}));
        attachedView = View.inflate(this,R.layout.listview_item,null);
        rootRl = (RelativeLayout) findViewById(R.id.root);
        testTv = (TextView)findViewById(R.id.item_tv_ss);
        lp = new RelativeLayout.LayoutParams(testTv.getLayoutParams());

        testListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
                count = 0;
                sampleImg = captureScreen(TestActivity.this);
                imageUp.setVisibility(View.VISIBLE);
                imageDown.setVisibility(View.VISIBLE);
                imageUp.setImageBitmap(sampleImg);
                gaussianBlurImg = blur(sampleImg, 25f);// 高斯模糊圖片
                imageDown.setImageBitmap(gaussianBlurImg);
                imageUp.setImageAlpha(255);
                HashMap item = (HashMap)testListView.getItemAtPosition(i);
                String section =String.valueOf(item.get("name").toString());//get每一行的數據的名字
                testTv.setText(section);
                int[] location = new int[2] ;
                testListView.getChildAt(i-testListView.getFirstVisiblePosition()).getLocationOnScreen(location);
                lp.setMargins(0, location[1], 0, 0);
                testTv.setLayoutParams(lp);
                testTv.setVisibility(View.VISIBLE);
                is3DTouch = true;

                return false;
            }
        });

        testListView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                switch (motionEvent.getAction()) {

                    case MotionEvent.ACTION_DOWN:
                        break;
                    case MotionEvent.ACTION_MOVE:
                        if(is3DTouch){
                            if (count < 255)
                                count = count + 8;
                            int alpha = 255 - count;
                            if (alpha < 0)
                                alpha = 0;
                            imageUp.setImageAlpha(alpha);
                        }
                        break;
                    case MotionEvent.ACTION_UP:
                            if(is3DTouch){
                                imageUp.setImageAlpha(255);
                                testTv.setVisibility(View.GONE);
                                imageUp.setVisibility(View.GONE);
                                imageDown.setVisibility(View.GONE);
                                is3DTouch = false;
                            }

                        break;
                }
                return false;
            }
        });
    }

    private void initData() {
        data = new ArrayList<Map<String, String>>();
        for (int i = 0; i < 20; i++) {
            Map<String, String> map = new HashMap<String, String>();
            map.put("name", "BYXD" + i);
            data.add(map);
        }
    }

    // 高斯模糊處理
    private Bitmap blur(Bitmap bitmap, float radius) {
        Bitmap output = Bitmap.createBitmap(bitmap); // 創建輸出圖片
        RenderScript rs = RenderScript.create(this); // 構建一個RenderScript對象
        ScriptIntrinsicBlur gaussianBlue = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs)); //
        // 創建高斯模糊腳本
        Allocation allIn = Allocation.createFromBitmap(rs, bitmap); // 開闢輸入內存
        Allocation allOut = Allocation.createFromBitmap(rs, output); // 開闢輸出內存
        gaussianBlue.setRadius(radius); // 設置模糊半徑,範圍0f<radius<=25f
        gaussianBlue.setInput(allIn); // 設置輸入內存
        gaussianBlue.forEach(allOut); // 模糊編碼,並將內存填入輸出內存
        allOut.copyTo(output); // 將輸出內存編碼爲Bitmap,圖片大小必須注意
        rs.destroy(); // 關閉RenderScript對象,API>=23則使用rs.releaseAllContexts()
        return output;
    }

    // 獲取屏幕照片
    public static Bitmap captureScreen(Activity activity) {

        activity.getWindow().getDecorView().setDrawingCacheEnabled(true);

        Bitmap bmp = activity.getWindow().getDecorView().getDrawingCache();

        return bmp;

    }

}

到這裏就實現了listview動態高斯模糊效果了。接下來對代碼進行優化,將效果調到iphone通訊錄那樣,在該效果後面加個popupmenu,這個有空貼代碼。



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章