Skip to content
This repository was archived by the owner on Nov 14, 2018. It is now read-only.

Commit 3253c2e

Browse files
LanderlYoungromainguy
authored andcommitted
fix issue #500 View.toBitmap should take scroll state into consideration (#504)
* fix issue #500 View.toBitmap should take scroll state into consideration * add toBitmapScrolls test case * hardcode test string * remove canvas save/restore simple bitmap compare * format file header
1 parent b7dfc03 commit 3253c2e

3 files changed

Lines changed: 71 additions & 1 deletion

File tree

src/androidTest/java/androidx/core/view/ViewTest.kt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
package androidx.core.view
1818

1919
import android.graphics.Bitmap
20+
import android.graphics.Color
2021
import android.support.test.InstrumentationRegistry
22+
import android.view.LayoutInflater
2123
import android.view.View
2224
import android.view.ViewGroup
2325
import android.widget.LinearLayout
@@ -175,6 +177,29 @@ class ViewTest {
175177
assertSame(Bitmap.Config.RGB_565, bitmap.config)
176178
}
177179

180+
@Test
181+
fun toBitmapScrolls() {
182+
val scrollView = LayoutInflater.from(context)!!
183+
.inflate(R.layout.test_bitmap_scrolls, null, false)
184+
185+
val size = 100
186+
187+
scrollView.measure(
188+
View.MeasureSpec.makeMeasureSpec(size, View.MeasureSpec.EXACTLY),
189+
View.MeasureSpec.makeMeasureSpec(size, View.MeasureSpec.EXACTLY))
190+
scrollView.layout(0, 0, size, size)
191+
192+
val noScroll = scrollView.toBitmap()
193+
assertEquals(Color.WHITE, noScroll.getPixel(0, 0))
194+
assertEquals(Color.WHITE, noScroll.getPixel(size - 1, size - 1))
195+
196+
scrollView.scrollTo(0, size)
197+
val scrolls = scrollView.toBitmap()
198+
199+
assertEquals(Color.BLACK, scrolls.getPixel(0, 0))
200+
assertEquals(Color.BLACK, scrolls.getPixel(size - 1, size - 1))
201+
}
202+
178203
@Test fun isVisible() {
179204
view.isVisible = true
180205
assertTrue(view.isVisible)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
~ Copyright (C) 2018 The Android Open Source Project
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ http://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
-->
17+
18+
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
19+
android:orientation="vertical"
20+
android:layout_width="100px"
21+
android:layout_height="100px"
22+
android:scrollbars="none"
23+
>
24+
25+
<LinearLayout
26+
android:layout_width="100px"
27+
android:layout_height="wrap_content"
28+
android:orientation="vertical">
29+
30+
<View
31+
android:layout_width="match_parent"
32+
android:layout_height="100px"
33+
android:background="@android:color/white" />
34+
35+
<View
36+
android:layout_width="match_parent"
37+
android:layout_height="100px"
38+
android:background="@android:color/black" />
39+
40+
</LinearLayout>
41+
42+
</ScrollView>

src/main/java/androidx/core/view/View.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,10 @@ fun View.toBitmap(config: Bitmap.Config = Bitmap.Config.ARGB_8888): Bitmap {
196196
if (!ViewCompat.isLaidOut(this)) {
197197
throw IllegalStateException("View needs to be laid out before calling toBitmap()")
198198
}
199-
return Bitmap.createBitmap(width, height, config).applyCanvas(::draw)
199+
return Bitmap.createBitmap(width, height, config).applyCanvas {
200+
translate(-scrollX.toFloat(), -scrollY.toFloat())
201+
draw(this)
202+
}
200203
}
201204

202205
/**

0 commit comments

Comments
 (0)