Skip to content Skip to sidebar Skip to footer

How To Capture Part Of A Scrollview As Its Content Is Scrolling?

After extending the ScrollView class I was able to easily be notified of the scrolling in realtime. Now I need to capture the content of this scrollview in a very specific part. Le

Solution 1:

Since the problem was fun I implemented it, it seems to work fine. I guess that you are recreating a Bitmap each time that's why goes slow. The idea is like this, you create an area in the ScrollView that you want to copy (see Rect cropRect and Bitmap screenshotBitmap), it's full width and you just need to set the height. The view automatically set a scroll listener on itself and on every scroll it will copy that area. Note that setDrawingCacheEanbled(true) is called just once when the view is instantiated, it basically tells the view that you will call getDrawingCache(), which will return the Bitmap on which the view is drawing itself. It then copy the area of interest on screenshotBitmap and that's the Bitmap that you might want to use.

ScreenshottableScrollView.java

package com.example.lelloman.screenshottablescrollview;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.os.Build;
import android.util.AttributeSet;
import android.view.ViewTreeObserver;
import android.widget.ScrollView;

/**
 * Created by lelloman on 16-2-16.
 */publicclassScreenshottableScrollViewextendsScrollViewimplementsViewTreeObserver.OnScrollChangedListener {

    publicinterfaceOnNewScreenshotListener {
        voidonNewScreenshot(Bitmap bitmap);
    }

    privateBitmapscreenshotBitmap=null;
    privateCanvasscreenshotCanvas=null;
    privateintscreenshotHeightPx=0;
    privateOnNewScreenshotListenerlistener=null;
    private Rect cropRect;
    privatePaintpaint=newPaint(Paint.ANTI_ALIAS_FLAG);


    publicScreenshottableScrollView(Context context) {
        super(context);
        init();
    }

    publicScreenshottableScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    publicScreenshottableScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)publicScreenshottableScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    privatevoidinit(){
        setDrawingCacheEnabled(true);
        getViewTreeObserver().addOnScrollChangedListener(this);
    }

    publicvoidsetOnNewScreenshotListener(OnNewScreenshotListener listener){
        this.listener = listener;
    }

    @OverrideprotectedvoidonSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        if(screenshotHeightPx != 0)
            makeScrenshotBitmap(w,h);
    }

    publicvoidsetScreenshotHeightPx(int q){
        screenshotHeightPx = q;
        makeScrenshotBitmap(getWidth(), getHeight());
    }

    privatevoidmakeScrenshotBitmap(int width, int height){

        if(screenshotBitmap != null) screenshotBitmap.recycle();

        if(width == 0 || height == 0) return;

        screenshotBitmap = Bitmap.createBitmap(width, screenshotHeightPx, Bitmap.Config.ARGB_8888);
        screenshotCanvas = newCanvas(screenshotBitmap);

        cropRect = newRect(0,0,width,screenshotHeightPx);


    }

    @OverridepublicvoidonScrollChanged() {
        if(listener == null) return;

        Bitmapbitmap= getDrawingCache();

        screenshotCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);

        screenshotCanvas.drawBitmap(bitmap,cropRect, cropRect,paint);
        listener.onNewScreenshot(screenshotBitmap);
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns: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"android:orientation="vertical"tools:context="com.example.lelloman.screenshottablescrollview.MainActivity"><com.example.lelloman.screenshottablescrollview.ScreenshottableScrollViewandroid:id="@+id/scrollView"android:layout_weight="1"android:layout_width="match_parent"android:layout_height="0dp"><TextViewandroid:id="@+id/textView"android:layout_width="match_parent"android:layout_height="wrap_content"/></com.example.lelloman.screenshottablescrollview.ScreenshottableScrollView><Viewandroid:layout_width="match_parent"android:layout_height="20dp"android:background="#ff000000"/><ImageViewandroid:id="@+id/imageView"android:layout_width="match_parent"android:layout_height="100dp" /></LinearLayout>

MainActivity.java

package com.example.lelloman.screenshottablescrollview;

import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.Random;

publicclassMainActivityextendsAppCompatActivity {

    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        StringBuilderbuilder=newStringBuilder();

        Randomrandom=newRandom();
        StringAB="abcdefghijklmnopqrstuvwxyz ";

        for(int i=0;i<100;i++){
            builder.append("\n\n"+Integer.toString(i)+"\n\n");
            for(intj=0;j<1000;j++){
                builder.append(AB.charAt(random.nextInt(AB.length())));
            }
        }

        ((TextView) findViewById(R.id.textView)).setText(builder.toString());
        finalImageViewimageView= (ImageView) findViewById(R.id.imageView);

        ScreenshottableScrollViewscrollView= (ScreenshottableScrollView) findViewById(R.id.scrollView);
        scrollView.setScreenshotHeightPx((int) (getResources().getDisplayMetrics().density * 100));

        scrollView.setOnNewScreenshotListener(newScreenshottableScrollView.OnNewScreenshotListener() {
            @OverridepublicvoidonNewScreenshot(Bitmap bitmap) {
                Log.d("MainActivity","onNewScreenshot");
                imageView.setImageBitmap(bitmap);
            }
        });
    }
}

Post a Comment for "How To Capture Part Of A Scrollview As Its Content Is Scrolling?"