android长截屏beta1

来源:互联网 发布:北通 手柄 知乎 编辑:程序博客网 时间:2024/04/30 12:16
package com.example.wanjian.test;import android.graphics.Bitmap;import android.graphics.Canvas;import android.graphics.Color;import android.os.Environment;import android.os.SystemClock;import android.view.MotionEvent;import android.view.View;import android.view.ViewConfiguration;import android.widget.LinearLayout;import android.widget.Toast;import java.io.File;import java.io.FileOutputStream;import java.lang.ref.WeakReference;import java.util.ArrayList;import java.util.List;/** * Created by wanjian on 16/8/18. */public class ScrollableViewRECUtil {    public static final int VERTICAL = 0;    private static final int DELAY = 2;    private List<Bitmap> bitmaps = new ArrayList<>();    private int orientation = VERTICAL;    private View view;    private boolean isEnd;    private OnRecFinishedListener listener;    public ScrollableViewRECUtil(View view, int orientation) {        this.view = view;        this.orientation = orientation;    }    public void start(final OnRecFinishedListener listener) {        this.listener = listener;        final MotionEvent motionEvent = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, view.getWidth() / 2, view.getHeight() / 2, 0);        view.dispatchTouchEvent(motionEvent);        motionEvent.setAction(MotionEvent.ACTION_MOVE);        //滑动距离大于ViewConfiguration.get(view.getContext()).getScaledTouchSlop()时listview才开始滚动        motionEvent.setLocation(motionEvent.getX(), motionEvent.getY() - (ViewConfiguration.get(view.getContext()).getScaledTouchSlop() + 1));        view.dispatchTouchEvent(motionEvent);        motionEvent.setLocation(motionEvent.getX(), view.getHeight() / 2);        view.postDelayed(new Runnable() {            @Override            public void run() {                if (isEnd) {                    //停止时正好一屏则全部绘制,否则绘制部分                    if ((view.getHeight() / 2 - (int) motionEvent.getY()) % view.getHeight() == 0) {                        Bitmap bitmap = rec();                        bitmaps.add(bitmap);                    } else {                        Bitmap origBitmap = rec();                        int y = view.getHeight() / 2 - (int) motionEvent.getY();                        Bitmap bitmap = Bitmap.createBitmap(origBitmap, 0, view.getHeight() - y % view.getHeight(), view.getWidth(), y % view.getHeight());                        bitmaps.add(bitmap);                        origBitmap.recycle();                    }                    //最后一张可能高度不足view的高度                    int h = view.getHeight() * (bitmaps.size() - 1);                    Bitmap bitmap = bitmaps.get(bitmaps.size() - 1);                    h = h + bitmap.getHeight();                    Bitmap result = Bitmap.createBitmap(view.getWidth(), h, Bitmap.Config.RGB_565);                    Canvas canvas = new Canvas();                    canvas.setBitmap(result);                    for (int i = 0; i < bitmaps.size(); i++) {                        Bitmap b = bitmaps.get(i);                        canvas.drawBitmap(b, 0, i * view.getHeight(), null);                        b.recycle();                    }                    listener.onRecFinish(result);                    return;                }                if ((view.getHeight() / 2 - (int) motionEvent.getY()) % view.getHeight() == 0) {                    Bitmap bitmap = rec();                    bitmaps.add(bitmap);                }                motionEvent.setAction(MotionEvent.ACTION_MOVE);                motionEvent.setLocation((int) motionEvent.getX(), (int) motionEvent.getY() - 1);                view.dispatchTouchEvent(motionEvent);                view.postDelayed(this, DELAY);            }        }, DELAY);    }    public void stop() {        isEnd = true;    }    private Bitmap rec() {        Bitmap film = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.RGB_565);        Canvas canvas = new Canvas();        canvas.setBitmap(film);        view.draw(canvas);        return film;    }    public interface OnRecFinishedListener {        void onRecFinish(Bitmap bitmap);    }}
activity 代码


  setContentView(R.layout.activity_main4);//        listview= (ListView) findViewById(R.id.listview);        listview.setAdapter(new BaseAdapter() {            @Override            public int getCount() {                return 100;            }            @Override            public Object getItem(int position) {                return null;            }            @Override            public long getItemId(int position) {                return 0;            }            @Override            public View getView(int position, View convertView, ViewGroup parent) {                if (convertView==null){                    Button button= (Button) LayoutInflater.from(getApplication()).inflate(R.layout.item,listview,false);                    button.setText(""+position);                    return button;                }                ((Button)convertView).setText(""+position);                return convertView;            }        });//        File file=new File(Environment.getExternalStorageDirectory(),"aaa");        file.mkdirs();        for (File f:file.listFiles()){            f.delete();        }        listview.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {            @Override            public void onGlobalLayout() {                listview.getViewTreeObserver().removeGlobalOnLayoutListener(this);                start();            }        });

 private void start(){        final View view=findViewById(R.id.view);        final ScrollableViewRECUtil scrollableViewRECUtil=new ScrollableViewRECUtil(view,ScrollableViewRECUtil.VERTICAL);        scrollableViewRECUtil.start(new ScrollableViewRECUtil.OnRecFinishedListener() {            @Override            public void onRecFinish(Bitmap bitmap) {                File f= Environment.getExternalStorageDirectory();                System.out.print(f.getAbsoluteFile().toString());                Toast.makeText(getApplicationContext(),f.getAbsolutePath(),Toast.LENGTH_LONG).show();                try {                    bitmap.compress(Bitmap.CompressFormat.JPEG,60,new FileOutputStream(new File(f,"aaa/rec"+System.currentTimeMillis()+".jpg")));                    Toast.makeText(getApplicationContext(),"Success",Toast.LENGTH_LONG).show();                }catch (Exception e){                    e.printStackTrace();                }            }        });        // scrollableViewRECUtil        view.postDelayed(new Runnable() {            @Override            public void run() {                scrollableViewRECUtil.stop();            }        },90*1000);    }


布局代码

<?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:id="@+id/view"    android:orientation="vertical" >        <ListView            android:id="@+id/listview"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:divider="#e1e1e1"            android:dividerHeight="2dp"            ></ListView></LinearLayout>


效果图  图1是屏幕   图2是截屏







0 0
原创粉丝点击