Skip to content

Commit

Permalink
修复查看大图时崩溃的问题,优化应用启动速度,删除无用导包,优化代码逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
880634 committed May 7, 2019
1 parent fe7162d commit 9256ddd
Show file tree
Hide file tree
Showing 25 changed files with 190 additions and 174 deletions.
Binary file modified AndroidProject.apk
Binary file not shown.
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@
android:label="@string/web_title" />

<!-- 查看大图界面 -->
<activity android:name=".ui.activity.ImageActivity" />
<activity android:name=".ui.activity.PhotoActivity" />

<!-- 对话框界面 -->
<activity android:name=".ui.activity.DialogActivity" />
Expand Down
14 changes: 7 additions & 7 deletions app/src/main/java/com/hjq/demo/ui/activity/LauncherActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ private void initStartAnim() {
mTextView.startAnimation(ra);
}

private void requestFilePermission() {
private void requestPermission() {
XXPermissions.with(this)
.permission(Permission.Group.STORAGE)
.request(this);
Expand All @@ -102,14 +102,14 @@ public void noPermission(List<String> denied, boolean quick) {
if (quick) {
toast("没有权限访问文件,请手动授予权限");
XXPermissions.gotoPermissionSettings(LauncherActivity.this, true);
}else {
} else {
toast("请先授予文件读写权限");
getWindow().getDecorView().postDelayed(new Runnable() {
postDelayed(new Runnable() {
@Override
public void run() {
requestFilePermission();
requestPermission();
}
}, 2000);
}, 1000);
}
}

Expand All @@ -125,7 +125,7 @@ protected void onRestart() {
if (XXPermissions.isHasPermission(LauncherActivity.this, Permission.Group.STORAGE)) {
hasPermission(null, true);
}else {
requestFilePermission();
requestPermission();
}
}

Expand All @@ -144,7 +144,7 @@ public void onAnimationStart(Animation animation) {}

@Override
public void onAnimationEnd(Animation animation) {
requestFilePermission();
requestPermission();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* author : Android 轮子哥
* github : https://github.com/getActivity/AndroidProject
* time : 2019/04/20
* desc : 个人资料界面
* desc : 个人资料
*/
public final class PersonalDataActivity extends MyActivity {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,52 +1,52 @@
package com.hjq.demo.ui.activity;

import android.support.v4.view.ViewPager;

import com.hjq.demo.R;
import com.hjq.demo.common.MyActivity;
import com.hjq.demo.ui.adapter.ImagePagerAdapter;

import java.util.ArrayList;

import butterknife.BindView;

/**
* author : Android 轮子哥
* github : https://github.com/getActivity/AndroidProject
* time : 2019/03/05
* desc : 查看大图
*/
public final class ImageActivity extends MyActivity {

@BindView(R.id.vp_image_pager)
ViewPager mViewPager;

@Override
protected int getLayoutId() {
return R.layout.activity_image;
}

@Override
protected int getTitleId() {
return 0;
}

@Override
protected void initView() {

}

@Override
protected void initData() {
ArrayList<String> data = new ArrayList<>();
data.add("https://www.baidu.com/img/bd_logo.png");
data.add("https://www.baidu.com/img/bd_logo.png");
data.add("https://www.baidu.com/img/bd_logo.png");
mViewPager.setAdapter(new ImagePagerAdapter(this, data));
}

@Override
public boolean statusBarDarkFont() {
return !super.statusBarDarkFont();
}
package com.hjq.demo.ui.activity;

import android.support.v4.view.ViewPager;

import com.hjq.demo.R;
import com.hjq.demo.common.MyActivity;
import com.hjq.demo.ui.adapter.PhotoPagerAdapter;

import java.util.ArrayList;

import butterknife.BindView;

/**
* author : Android 轮子哥
* github : https://github.com/getActivity/AndroidProject
* time : 2019/03/05
* desc : 查看大图
*/
public final class PhotoActivity extends MyActivity {

@BindView(R.id.vp_photo_pager)
ViewPager mViewPager;

@Override
protected int getLayoutId() {
return R.layout.activity_photo;
}

@Override
protected int getTitleId() {
return 0;
}

@Override
protected void initView() {

}

@Override
protected void initData() {
ArrayList<String> data = new ArrayList<>();
data.add("https://www.baidu.com/img/bd_logo.png");
data.add("https://www.baidu.com/img/bd_logo.png");
data.add("https://www.baidu.com/img/bd_logo.png");
mViewPager.setAdapter(new PhotoPagerAdapter(this, data));
}

@Override
public boolean statusBarDarkFont() {
return !super.statusBarDarkFont();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import com.hjq.demo.common.MyActivity;
import com.hjq.demo.helper.ActivityStackManager;
import com.hjq.demo.helper.CacheDataManager;
import com.hjq.widget.SettingBar;
import com.hjq.image.ImageLoader;
import com.hjq.widget.SettingBar;
import com.hjq.widget.SwitchButton;

import butterknife.BindView;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ protected void initView() {
}

// 加快HTML网页加载完成的速度,等页面finish再加载图片
if(Build.VERSION.SDK_INT >= 19) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
settings.setLoadsImagesAutomatically(true);
} else {
settings.setLoadsImagesAutomatically(false);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,71 +1,71 @@
package com.hjq.demo.ui.adapter;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;

import com.github.chrisbanes.photoview.PhotoView;
import com.hjq.image.ImageLoader;

import java.util.List;

/**
* author : Android 轮子哥
* github : https://github.com/getActivity/AndroidProject
* time : 2019/03/05
* desc : 图片加载适配器
*/
public final class ImagePagerAdapter extends PagerAdapter implements View.OnClickListener {

private Activity mActivity;
private List<String> mData;

public ImagePagerAdapter(Activity activity, List<String> data) {
mActivity = activity;
mData = data;
}

// 加载数量,自动回调
@Override
public int getCount() {
return mData.size();
}

// 返回真表示不会重新创建,使用缓存加载。返回假则重新创建
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view == object;
}

/**
* 实例化条目
* ViewPager预加载机制:最多保存3个page,超过的将需要被销毁掉
* 由于最多3个page,所以不需要设置ViewHolder
* 用于将数据设置给ViewItem
*/

@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
PhotoView view = new PhotoView(mActivity);
view.setOnClickListener(this);
ImageLoader.loadImage(view, mData.get(position));
// 将View添加到ViewPager
container.addView(view);
return view;
}

// 销毁条目
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((View) object);
}

@Override
public void onClick(View v) {
// 点击退出当前的 Activity
mActivity.finish();
}
package com.hjq.demo.ui.adapter;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;

import com.github.chrisbanes.photoview.PhotoView;
import com.hjq.image.ImageLoader;

import java.util.List;

/**
* author : Android 轮子哥
* github : https://github.com/getActivity/AndroidProject
* time : 2019/03/05
* desc : 图片加载适配器
*/
public final class PhotoPagerAdapter extends PagerAdapter implements View.OnClickListener {

private Activity mActivity;
private List<String> mData;

public PhotoPagerAdapter(Activity activity, List<String> data) {
mActivity = activity;
mData = data;
}

// 加载数量,自动回调
@Override
public int getCount() {
return mData.size();
}

// 返回真表示不会重新创建,使用缓存加载。返回假则重新创建
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view == object;
}

/**
* 实例化条目
* ViewPager预加载机制:最多保存3个page,超过的将需要被销毁掉
* 由于最多3个page,所以不需要设置ViewHolder
* 用于将数据设置给ViewItem
*/

@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
PhotoView view = new PhotoView(mActivity);
view.setOnClickListener(this);
ImageLoader.loadImage(view, mData.get(position));
// 将View添加到ViewPager
container.addView(view);
return view;
}

// 销毁条目
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((View) object);
}

@Override
public void onClick(View v) {
// 点击退出当前的 Activity
mActivity.finish();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import android.support.design.widget.AppBarLayout;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.widget.TextView;

import com.gyf.barlibrary.ImmersionBar;
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/hjq/demo/ui/fragment/TestFragmentD.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import com.hjq.demo.ui.activity.AboutActivity;
import com.hjq.demo.ui.activity.DialogActivity;
import com.hjq.demo.ui.activity.HomeActivity;
import com.hjq.demo.ui.activity.ImageActivity;
import com.hjq.demo.ui.activity.PhotoActivity;
import com.hjq.demo.ui.activity.LoginActivity;
import com.hjq.demo.ui.activity.PasswordForgetActivity;
import com.hjq.demo.ui.activity.PasswordResetActivity;
Expand Down Expand Up @@ -99,7 +99,7 @@ public void onClick(View v) {
startActivity(WebActivity.class);
break;
case R.id.btn_test_image:
startActivity(ImageActivity.class);
startActivity(PhotoActivity.class);
break;
case R.id.btn_test_pay:
new MessageDialog.Builder(getBindingActivity())
Expand Down
34 changes: 34 additions & 0 deletions app/src/main/java/com/hjq/demo/widget/PhotoViewPager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.hjq.demo.widget;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;

/**
* author : Android 轮子哥
* github : https://github.com/getActivity/AndroidProject
* time : 2019/05/07
* desc : ViewPager 中使用 PhotoView 时出现 pointerIndex out of range 异常
*/
public final class PhotoViewPager extends ViewPager {

public PhotoViewPager(Context context) {
super(context);
}

public PhotoViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
// 当PhotoView 和 ViewPager 组合时 ,用双指进行放大时 是没有问题的,但是用双指进行缩小的时候,程序就会崩掉
// 并且抛出java.lang.IllegalArgumentException: pointerIndex out of range
try {
return super.onInterceptTouchEvent(ev);
} catch (IllegalArgumentException | ArrayIndexOutOfBoundsException ignored) {
return false;
}
}
}
File renamed without changes
File renamed without changes
Loading

0 comments on commit 9256ddd

Please sign in to comment.