Add an error view when loading the image fails in ViewImageActivity.

This commit is contained in:
Alex Ning 2018-10-01 13:45:02 +08:00
parent 1bd9ba61c6
commit 17ccccfa0e
5 changed files with 116 additions and 38 deletions

View File

@ -28,7 +28,8 @@
</option> </option>
<option name="values"> <option name="values">
<map> <map>
<entry key="outputName" value="ic_error_outline_black_24dp" /> <entry key="color" value="ffffff" />
<entry key="outputName" value="ic_error_outline_white_24dp" />
<entry key="sourceFile" value="$USER_HOME$" /> <entry key="sourceFile" value="$USER_HOME$" />
</map> </map>
</option> </option>

View File

@ -24,10 +24,12 @@ import android.support.v7.app.AppCompatActivity;
import android.text.Spannable; import android.text.Spannable;
import android.text.SpannableString; import android.text.SpannableString;
import android.text.style.ForegroundColorSpan; import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.MotionEvent; import android.view.MotionEvent;
import android.view.View; import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar; import android.widget.ProgressBar;
import android.widget.RelativeLayout; import android.widget.RelativeLayout;
import android.widget.Toast; import android.widget.Toast;
@ -59,6 +61,10 @@ public class ViewImageActivity extends AppCompatActivity {
static final String IMAGE_URL_KEY = "IUK"; static final String IMAGE_URL_KEY = "IUK";
static final String FILE_NAME_KEY = "FNK"; static final String FILE_NAME_KEY = "FNK";
private ProgressBar mProgressBar;
private GestureImageView mImageView;
private LinearLayout mLoadErrorLinearLayout;
private boolean isActionBarHidden = false; private boolean isActionBarHidden = false;
private boolean isDownloading = false; private boolean isDownloading = false;
@ -72,6 +78,8 @@ public class ViewImageActivity extends AppCompatActivity {
private float touchY = -1.0f; private float touchY = -1.0f;
private float zoom = 1.0f; private float zoom = 1.0f;
private boolean isSwiping = false;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@ -88,8 +96,20 @@ public class ViewImageActivity extends AppCompatActivity {
setTitle(text); setTitle(text);
final RelativeLayout relativeLayout = findViewById(R.id.parent_relative_layout_view_image_activity); final RelativeLayout relativeLayout = findViewById(R.id.parent_relative_layout_view_image_activity);
final GestureImageView imageView = findViewById(R.id.image_view_view_image_activity); mImageView = findViewById(R.id.image_view_view_image_activity);
final ProgressBar progressBar = findViewById(R.id.progress_bar_view_image_activity); mProgressBar = findViewById(R.id.progress_bar_view_image_activity);
mLoadErrorLinearLayout = findViewById(R.id.load_image_error_linear_layout_view_image_activity);
mLoadErrorLinearLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(!isSwiping) {
mProgressBar.setVisibility(View.VISIBLE);
mLoadErrorLinearLayout.setVisibility(View.GONE);
loadImage();
}
}
});
final float pxHeight = getResources().getDisplayMetrics().heightPixels; final float pxHeight = getResources().getDisplayMetrics().heightPixels;
@ -134,10 +154,13 @@ public class ViewImageActivity extends AppCompatActivity {
} }
}); });
loadImage();
swipe = new Swipe(); swipe = new Swipe();
swipe.setListener(new SimpleSwipeListener() { swipe.setListener(new SimpleSwipeListener() {
@Override @Override
public void onSwipingUp(final MotionEvent event) { public void onSwipingUp(final MotionEvent event) {
isSwiping = true;
float nowY = event.getY(); float nowY = event.getY();
float offset; float offset;
if (touchY == -1.0f) { if (touchY == -1.0f) {
@ -147,21 +170,22 @@ public class ViewImageActivity extends AppCompatActivity {
} }
totalLengthY += offset; totalLengthY += offset;
touchY = nowY; touchY = nowY;
imageView.animate() mImageView.animate()
.y(totalLengthY) .y(totalLengthY)
.setDuration(0) .setDuration(0)
.start(); .start();
mLoadErrorLinearLayout.animate()
.y(totalLengthY)
.setDuration(0)
.start();
Log.i("total length", Float.toString(totalLengthY));
} }
@Override @Override
public boolean onSwipedUp(final MotionEvent event) { public boolean onSwipedUp(final MotionEvent event) {
imageView.animate() Log.i("total length", Float.toString(totalLengthY));
.y(0)
.setDuration(300)
.start();
if (totalLengthY < -pxHeight / 8) { if (totalLengthY < -pxHeight / 8) {
imageView.animate() mImageView.animate()
.y(-pxHeight) .y(-pxHeight)
.setDuration(300) .setDuration(300)
.setListener(new Animator.AnimatorListener() { .setListener(new Animator.AnimatorListener() {
@ -186,8 +210,17 @@ public class ViewImageActivity extends AppCompatActivity {
} }
}) })
.start(); .start();
mLoadErrorLinearLayout.animate()
.y(-pxHeight)
.setDuration(300)
.start();
} else { } else {
imageView.animate() isSwiping = false;
mImageView.animate()
.y(0)
.setDuration(300)
.start();
mLoadErrorLinearLayout.animate()
.y(0) .y(0)
.setDuration(300) .setDuration(300)
.start(); .start();
@ -200,6 +233,7 @@ public class ViewImageActivity extends AppCompatActivity {
@Override @Override
public void onSwipingDown(final MotionEvent event) { public void onSwipingDown(final MotionEvent event) {
isSwiping = true;
float nowY = event.getY(); float nowY = event.getY();
float offset; float offset;
if (touchY == -1.0f) { if (touchY == -1.0f) {
@ -209,7 +243,11 @@ public class ViewImageActivity extends AppCompatActivity {
} }
totalLengthY += offset; totalLengthY += offset;
touchY = nowY; touchY = nowY;
imageView.animate() mImageView.animate()
.y(totalLengthY)
.setDuration(0)
.start();
mLoadErrorLinearLayout.animate()
.y(totalLengthY) .y(totalLengthY)
.setDuration(0) .setDuration(0)
.start(); .start();
@ -218,7 +256,7 @@ public class ViewImageActivity extends AppCompatActivity {
@Override @Override
public boolean onSwipedDown(final MotionEvent event) { public boolean onSwipedDown(final MotionEvent event) {
if (totalLengthY > pxHeight / 8) { if (totalLengthY > pxHeight / 8) {
imageView.animate() mImageView.animate()
.y(pxHeight) .y(pxHeight)
.setDuration(300) .setDuration(300)
.setListener(new Animator.AnimatorListener() { .setListener(new Animator.AnimatorListener() {
@ -243,8 +281,17 @@ public class ViewImageActivity extends AppCompatActivity {
} }
}) })
.start(); .start();
mLoadErrorLinearLayout.animate()
.y(pxHeight)
.setDuration(300)
.start();
} else { } else {
imageView.animate() isSwiping = false;
mImageView.animate()
.y(0)
.setDuration(300)
.start();
mLoadErrorLinearLayout.animate()
.y(0) .y(0)
.setDuration(300) .setDuration(300)
.start(); .start();
@ -257,7 +304,7 @@ public class ViewImageActivity extends AppCompatActivity {
} }
}); });
imageView.getController().addOnStateChangeListener(new GestureController.OnStateChangeListener() { mImageView.getController().addOnStateChangeListener(new GestureController.OnStateChangeListener() {
@Override @Override
public void onStateChanged(State state) { public void onStateChanged(State state) {
zoom = state.getZoom(); zoom = state.getZoom();
@ -269,22 +316,9 @@ public class ViewImageActivity extends AppCompatActivity {
} }
}); });
imageView.getController().getSettings().setPanEnabled(true); mImageView.getController().getSettings().setPanEnabled(true);
Glide.with(this).load(mImageUrl).listener(new RequestListener<Drawable>() { mImageView.setOnClickListener(new View.OnClickListener() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
return false;
}
@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
progressBar.setVisibility(View.GONE);
return false;
}
}).apply(new RequestOptions().fitCenter()).into(imageView);
imageView.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
if (isActionBarHidden) { if (isActionBarHidden) {
@ -307,6 +341,23 @@ public class ViewImageActivity extends AppCompatActivity {
}); });
} }
private void loadImage() {
Glide.with(this).load(mImageUrl).listener(new RequestListener<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
mProgressBar.setVisibility(View.GONE);
mLoadErrorLinearLayout.setVisibility(View.VISIBLE);
return false;
}
@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
mProgressBar.setVisibility(View.GONE);
return false;
}
}).apply(new RequestOptions().fitCenter()).into(mImageView);
}
@Override @Override
public boolean onCreateOptionsMenu(Menu menu) { public boolean onCreateOptionsMenu(Menu menu) {
mMenu = menu; mMenu = menu;

View File

@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#FFFFFF"
android:viewportHeight="24.0" android:viewportWidth="24.0"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FF000000" android:pathData="M11,15h2v2h-2zM11,7h2v6h-2zM11.99,2C6.47,2 2,6.48 2,12s4.47,10 9.99,10C17.52,22 22,17.52 22,12S17.52,2 11.99,2zM12,20c-4.42,0 -8,-3.58 -8,-8s3.58,-8 8,-8 8,3.58 8,8 -3.58,8 -8,8z"/>
</vector>

View File

@ -20,4 +20,21 @@
android:layout_height="match_parent" android:layout_height="match_parent"
app:gest_fillViewport="true" /> app:gest_fillViewport="true" />
<LinearLayout
android:id="@+id/load_image_error_linear_layout_view_image_activity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableTop="@drawable/ic_error_outline_white_24dp"
android:layout_gravity="center"
android:gravity="center"
android:textColor="@android:color/white"
android:text="@string/tap_to_retry" />
</LinearLayout>
</RelativeLayout> </RelativeLayout>

View File

@ -11,6 +11,7 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical" android:orientation="vertical"
android:background="#FFFFFF"
android:id="@+id/linear_layout_view_post_detail"> android:id="@+id/linear_layout_view_post_detail">
<RelativeLayout <RelativeLayout
@ -18,9 +19,7 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginBottom="8dp" android:layout_marginBottom="8dp"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp" android:layout_marginStart="16dp"
android:layout_marginRight="16dp"
android:layout_marginEnd="16dp" android:layout_marginEnd="16dp"
android:layout_marginTop="16dp"> android:layout_marginTop="16dp">
@ -34,11 +33,13 @@
<TextView <TextView
android:id="@+id/subreddit_text_view_best_post_item" android:id="@+id/subreddit_text_view_best_post_item"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:layout_marginStart="16dp" android:layout_marginStart="16dp"
android:layout_marginEnd="8dp"
android:layout_toStartOf="@id/stickied_post_image_view_best_post_item" android:layout_toStartOf="@id/stickied_post_image_view_best_post_item"
android:layout_toEndOf="@id/subreddit_icon_circle_image_view_best_post_item" android:layout_toEndOf="@id/subreddit_icon_circle_image_view_best_post_item"
android:layout_centerVertical="true" android:layout_centerVertical="true"
android:gravity="center_vertical"
android:textColor="#E91E63"/> android:textColor="#E91E63"/>
<ImageView <ImageView
@ -48,14 +49,16 @@
android:layout_toStartOf="@id/post_time_text_view_best_post_item" android:layout_toStartOf="@id/post_time_text_view_best_post_item"
android:layout_marginStart="8dp" android:layout_marginStart="8dp"
android:layout_marginEnd="8dp" android:layout_marginEnd="8dp"
android:tint="@color/colorPrimary"/> android:tint="@color/colorPrimary"
android:visibility="gone"/>
<TextView <TextView
android:id="@+id/post_time_text_view_best_post_item" android:id="@+id/post_time_text_view_best_post_item"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:layout_alignParentEnd="true" android:layout_alignParentEnd="true"
android:layout_centerVertical="true"/> android:layout_centerVertical="true"
android:gravity="center_vertical"/>
</RelativeLayout> </RelativeLayout>
@ -82,7 +85,7 @@
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:background="@drawable/rounded_corner" android:background="@drawable/rounded_corner"
android:layout_marginEnd="16dp" android:layout_marginEnd="8dp"
android:layout_centerVertical="true" android:layout_centerVertical="true"
android:textColor="@android:color/white"/> android:textColor="@android:color/white"/>
@ -92,6 +95,7 @@
android:layout_height="24dp" android:layout_height="24dp"
android:layout_toEndOf="@id/type_text_view_item_best_post" android:layout_toEndOf="@id/type_text_view_item_best_post"
android:layout_centerVertical="true" android:layout_centerVertical="true"
android:layout_marginStart="8dp"
android:visibility="gone"/> android:visibility="gone"/>
<TextView <TextView
@ -224,6 +228,6 @@
<View <View
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="1dp" android:layout_height="1dp"
android:background="#E0E0E0"/> android:background="#F0F0F0"/>
</LinearLayout> </LinearLayout>