Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
B
beyond-clouds-android
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
云深项目组
beyond-clouds-android
Commits
5a94723a
Commit
5a94723a
authored
Mar 17, 2020
by
段启岩
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
自定义布局DynamicGridLayout优化
parent
cee53512
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
74 additions
and
25 deletions
+74
-25
app/src/main/java/cn/yunliyunwai/beyondclouds/view/DynamicGridLayout.java
+74
-24
app/src/main/res/layout/swipe_refresh_list.xml
+0
-1
No files found.
app/src/main/java/cn/yunliyunwai/beyondclouds/view/DynamicGridLayout.java
View file @
5a94723a
package
cn
.
yunliyunwai
.
beyondclouds
.
view
;
import
android.content.Context
;
import
android.graphics.drawable.BitmapDrawable
;
import
android.os.Build
;
import
android.util.AttributeSet
;
import
android.view.ViewGroup
;
import
android.widget.ImageView
;
import
androidx.annotation.Nullable
;
import
androidx.annotation.RequiresApi
;
import
com.bumptech.glide.Glide
;
import
com.bumptech.glide.Priority
;
import
com.bumptech.glide.RequestBuilder
;
import
com.bumptech.glide.load.DataSource
;
import
com.bumptech.glide.load.engine.DiskCacheStrategy
;
import
com.bumptech.glide.load.engine.GlideException
;
import
com.bumptech.glide.request.RequestListener
;
import
com.bumptech.glide.request.RequestOptions
;
import
com.bumptech.glide.request.target.Target
;
import
java.util.List
;
...
...
@@ -26,6 +33,8 @@ public class DynamicGridLayout extends ViewGroup {
private
int
mCol
;
private
int
mSpacing
;
List
<
String
>
mImgUrls
;
private
boolean
isImageInit
;
public
DynamicGridLayout
(
Context
context
)
{
...
...
@@ -54,7 +63,32 @@ public class DynamicGridLayout extends ViewGroup {
@Override
protected
void
onLayout
(
boolean
changed
,
int
l
,
int
t
,
int
r
,
int
b
)
{
if
(!
isImageInit
)
{
initImages
();
}
}
private
int
computePosition
(
int
directionIndex
,
int
imageSize
)
{
return
(
imageSize
+
mSpacing
)
*
directionIndex
;
}
public
void
setImages
(
List
<
String
>
imgUrls
)
{
removeAllViewsInLayout
();
this
.
mImgUrls
=
imgUrls
;
LayoutParams
layoutParams
=
getLayoutParams
();
isImageInit
=
false
;
if
(!
hasImages
())
{
layoutParams
.
height
=
0
;
setLayoutParams
(
layoutParams
);
return
;
}
initGridParams
();
requestLayout
();
}
private
void
initImages
()
{
if
(!
hasImages
())
{
return
;
}
...
...
@@ -64,6 +98,11 @@ public class DynamicGridLayout extends ViewGroup {
// 每个图片的尺寸
int
imageSize
=
(
maxWidth
-
(
mCol
-
1
)
*
mSpacing
)
/
mCol
;
if
(
getImageCount
()
==
1
)
{
imageSize
=
(
int
)
(
imageSize
*
0.66
);
}
int
layoutHeight
=
imageSize
*
mRow
+
(
mRow
-
1
)
*
mSpacing
;
LayoutParams
layoutParams
=
getLayoutParams
();
...
...
@@ -75,40 +114,51 @@ public class DynamicGridLayout extends ViewGroup {
for
(
int
i
=
0
;
i
<
mImgUrls
.
size
();
i
++)
{
currentRow
=
i
/
mCol
;
currentCol
=
i
-
(
currentRow
*
mCol
);
ImageView
imageView
=
new
ImageView
(
this
.
getContext
());
RequestOptions
options
=
new
RequestOptions
()
.
centerCrop
()
.
placeholder
(
R
.
drawable
.
placeholder_square
)
.
error
(
R
.
drawable
.
placeholder_square
)
.
priority
(
Priority
.
HIGH
)
.
diskCacheStrategy
(
DiskCacheStrategy
.
AUTOMATIC
);
Glide
.
with
(
this
).
load
(
mImgUrls
.
get
(
i
)).
apply
(
options
).
into
(
imageView
);
ImageView
imageView
=
createImageView
(
mImgUrls
.
get
(
i
));
this
.
addView
(
imageView
,
new
LayoutParams
(
imageSize
,
imageSize
));
int
left
=
computePosition
(
currentCol
,
imageSize
);
int
top
=
computePosition
(
currentRow
,
imageSize
);
imageView
.
setScaleType
(
ImageView
.
ScaleType
.
CENTER_CROP
);
imageView
.
layout
(
left
,
top
,
left
+
imageSize
,
top
+
imageSize
);
}
isImageInit
=
true
;
}
private
int
computePosition
(
int
directionIndex
,
int
imageSize
)
{
return
(
imageSize
+
mSpacing
)
*
directionIndex
;
}
public
void
setImages
(
List
<
String
>
imgUrls
)
{
removeAllViewsInLayout
();
this
.
mImgUrls
=
imgUrls
;
LayoutParams
layoutParams
=
getLayoutParams
();
private
ImageView
createImageView
(
String
imgUrl
)
{
if
(!
hasImages
())
{
layoutParams
.
height
=
0
;
setLayoutParams
(
layoutParams
);
return
;
ImageView
imageView
=
new
ImageView
(
this
.
getContext
());
if
(
getImageCount
()
==
1
)
{
imageView
.
setScaleType
(
ImageView
.
ScaleType
.
FIT_START
);
}
else
{
imageView
.
setScaleType
(
ImageView
.
ScaleType
.
CENTER_CROP
);
}
initGridParams
();
requestLayout
();
RequestOptions
options
=
new
RequestOptions
()
.
placeholder
(
R
.
drawable
.
placeholder_square
)
.
error
(
R
.
drawable
.
placeholder_square
)
.
priority
(
Priority
.
HIGH
)
.
diskCacheStrategy
(
DiskCacheStrategy
.
AUTOMATIC
);
RequestBuilder
builder
=
Glide
.
with
(
this
).
load
(
imgUrl
).
apply
(
options
);
if
(
getImageCount
()
==
1
)
{
builder
.
addListener
(
new
RequestListener
()
{
@Override
public
boolean
onLoadFailed
(
@Nullable
GlideException
e
,
Object
model
,
Target
target
,
boolean
isFirstResource
)
{
return
false
;
}
@Override
public
boolean
onResourceReady
(
Object
resource
,
Object
model
,
Target
target
,
DataSource
dataSource
,
boolean
isFirstResource
)
{
if
(
resource
instanceof
BitmapDrawable
)
{
LayoutParams
layoutParams
=
getLayoutParams
();
layoutParams
.
height
=
((
BitmapDrawable
)
resource
).
getBitmap
().
getHeight
();
setLayoutParams
(
layoutParams
);
}
return
false
;
}
});
}
builder
.
into
(
imageView
);
return
imageView
;
}
/**
...
...
app/src/main/res/layout/swipe_refresh_list.xml
View file @
5a94723a
...
...
@@ -15,7 +15,6 @@
<androidx.recyclerview.widget.RecyclerView
android:id=
"@+id/recycler_view"
app:fastScrollEnabled=
"false"
android:layout_width=
"match_parent"
android:layout_height=
"match_parent"
android:scrollbars=
"vertical"
/>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment