Skip to content

Commit

Permalink
新增查看文章图片
Browse files Browse the repository at this point in the history
新增
1.新增查看文章内的所有图片,左右滑动可切换
2.图片可放大缩小
  • Loading branch information
zhongzilu committed Oct 26, 2016
1 parent 108fff2 commit 09709f9
Show file tree
Hide file tree
Showing 19 changed files with 1,645 additions and 31 deletions.
15 changes: 9 additions & 6 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.zhongzilu.bit100">


<!-- 通用权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<uses-permission-sdk-23 android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission-sdk-23 android:name="android.permission.INTERNET" />

<uses-permission android:name="android.permission.MANAGE_DOCUMENTS" />

<uses-permission-sdk-23 android:name="android.permission.MANAGE_DOCUMENTS" />

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Expand All @@ -33,24 +33,27 @@
</intent-filter>
</activity>

<!--主Activity-->
<!-- 主Activity -->
<activity
android:name=".view.activity.Bit100MainActivity"
android:theme="@style/AppTheme.NoActionBar" />

<!--文章详情Activity-->
<!-- 文章详情Activity -->
<activity
android:name=".view.activity.Bit100ArticleDetailActivity"
android:theme="@style/AppTheme.NoActionBar" />

<!--设置Activity-->
<!-- 设置Activity -->
<activity android:name=".view.activity.SettingsActivity" />

<!--登录Activity-->
<!-- 登录Activity -->
<activity
android:name=".view.activity.Bit100LoginActivity"
android:theme="@style/AppTheme.NoActionBar" />

<activity
android:name=".view.activity.GalleryActivity"
android:label="@string/title_activity_gallery"
android:theme="@style/AppTheme.NoActionBar"></activity>
</application>

</manifest>
17 changes: 17 additions & 0 deletions app/src/main/assets/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,25 @@
<body>
</body>
<script type="text/javascript">

var img;
var imgSrc = [];
function show(text) {
document.body.innerHTML = text;
img = document.getElementsByTagName("img");
for(var i=0; i < img.length; i++){
img[i].setAttribute("onclick", "showImage("+i+")");
imgSrc.push(img[i].src);
}
}

function showImage(position){
try{
Android.showImages(imgSrc, position);
} catch(error){
alert(error);
}
}

</script>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.zhongzilu.bit100.application.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

/**
* 监听网络状态变化的广播接收器
* Created by zhongzilu on 2016-10-24.
*/
public class NetworkBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = "NetworkBroadcastReceiver==>";

@Override
public void onReceive(Context context, Intent intent) {
//获取手机的连接服务管理器,这里是连接管理器类
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// Network[] states = cm.getAllNetworks();
//
// if (states != null){
// for (Network work : states){
// NetworkInfo networkInfo = cm.getNetworkInfo(work);
//
// if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI &&
// networkInfo.getState().equals(NetworkInfo.State.CONNECTED )){
//
// return;
// }
// }
//
// //wifi和数据流量都没有打开,提示没有可用网络,引导用户打开任意网络
// }
//
// return;
// }

NetworkInfo.State wifiState = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
NetworkInfo.State mobileState = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
NetworkInfo.State mobile4G = cm.getNetworkInfo(ConnectivityManager.TYPE_WIMAX).getState();

if (wifiState == null && mobileState == null){
//wifi和数据流量都没有打开,提示没有可用网络,引导用户打开任意网络
return;
}

if (NetworkInfo.State.CONNECTED == wifiState){
//wifi连接可用,这时可以加载比较耗流量的内容,比如更高清的图片和视频等
return;
}

if (NetworkInfo.State.CONNECTED == mobile4G){
//4G网络可用,这时可以加载比较耗流量的内容,同时也通过缓存机制和其他机制
//优化网络请求频率以及数据加载以减少流量消耗
return;
}

if (NetworkInfo.State.CONNECTED == mobileState){
//数据流量可用,这时避免加载比较耗流量的内容,同时,可以通过缓存机制和其他机制
//优化网络请求频率以及数据加载以减少流量消耗
// return;
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
package com.zhongzilu.bit100.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.zhongzilu.bit100.R;
import com.zhongzilu.bit100.application.util.LogUtil;
import com.zhongzilu.bit100.widget.TouchImageView;

/**
* 图片画廊,用于查看网页上的图片
* Created by zhongzilu on 2016-09-16.
*/
public class GalleryActivity extends AppCompatActivity {
private static final String TAG = "GalleryActivity==>";

//Extra Tag
public static final String EXTRA_IMAGES_LIST = "images_list";
public static final String EXTRA_CURRENT_IMAGE_POSITION = "image_position";

//Extra Value
private String[] mList;
private int mChosePosition;

/**
* The {@link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {@link FragmentPagerAdapter} derivative, which will keep every
* loaded fragment in memory. If this becomes too memory intensive, it
* may be best to switch to a
* {@link android.support.v4.app.FragmentStatePagerAdapter}.
*/
private SectionsPagerAdapter mSectionsPagerAdapter;

/**
* The {@link ViewPager} that will host the section contents.
*/
private ViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gallery);

mViewPager = (ViewPager) findViewById(R.id.container);

Intent intent = getIntent();
if (intent != null){
mList = intent.getStringArrayExtra(EXTRA_IMAGES_LIST);
mChosePosition = intent.getIntExtra(EXTRA_CURRENT_IMAGE_POSITION, 0);
}

// 如果mList为空,则不再进行其他操作
if (mList == null) {
LogUtil.d(TAG, "onCreate: mList is null");
return;
}
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(), mList);
mViewPager.setOffscreenPageLimit(2);
// Set up the ViewPager with the sections adapter.
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.setCurrentItem(mChosePosition, true);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_gallery, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.action_settings:

break;
}

return super.onOptionsItemSelected(item);
}

/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
private static final String TAG = "PlaceholderFragment==>";
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_IMAGE_URL = "image_url";
private View contentView;

public PlaceholderFragment() {}

/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(String url) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putString(ARG_IMAGE_URL, url);
fragment.setArguments(args);
return fragment;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (contentView == null)
contentView = inflater.inflate(R.layout.fragment_gallery, container, false);
return contentView;
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

TouchImageView imageView = (TouchImageView) view.findViewById(R.id.img_touch_image);
String url = getArguments().getString(ARG_IMAGE_URL);
LogUtil.d(TAG, "onViewCreated: url==>" + url);
Glide.with(getContext())
.load(url)
.placeholder(R.drawable.image_default)
.error(R.drawable.image_default)
.into(imageView);
}
}

/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
private String[] imgSrc;

public SectionsPagerAdapter(FragmentManager fm, String[] imgS) {
super(fm);
this.imgSrc = imgS;
}

@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
return PlaceholderFragment.newInstance(imgSrc[position]);
}

@Override
public int getCount() {
// Show imgSrc length total pages.
return imgSrc.length;
}

@Override
public CharSequence getPageTitle(int position) {
return "";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.widget.NestedScrollView;
import android.view.LayoutInflater;
Expand All @@ -13,6 +14,7 @@
import android.view.View;
import android.view.ViewGroup;
import android.webkit.DownloadListener;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.widget.Toast;

Expand All @@ -24,6 +26,7 @@
import com.zhongzilu.bit100.application.util.NetworkUtil;
import com.zhongzilu.bit100.model.bean.ArticleDetailBean;
import com.zhongzilu.bit100.model.response.AllPostsResponse;
import com.zhongzilu.bit100.view.activity.GalleryActivity;
import com.zhongzilu.bit100.widget.CustomLoadingWebView;

import org.json.JSONException;
Expand Down Expand Up @@ -68,7 +71,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
return contentView;
}

@SuppressLint("JavascriptInterface")
@SuppressLint("AddJavascriptInterface")
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Expand All @@ -78,7 +81,7 @@ public void onViewCreated(View view, Bundle savedInstanceState) {
if (mWebView == null)
mWebView = (CustomLoadingWebView) view.findViewById(R.id.wv_article_detail_webView);
mScroll = (NestedScrollView) view.findViewById(R.id.nested_scroll_view);
mWebView.addJavascriptInterface(this, "Submit");
mWebView.addJavascriptInterface(this, "Android");
mWebView.setOnReloadListener(this);

initWebView();
Expand Down Expand Up @@ -152,7 +155,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
private void shareArticle(){
if (mBean == null)return;

Toast.makeText(getActivity(), R.string.toast_invoking_share, Toast.LENGTH_SHORT).show();
Snackbar.make(getView(), R.string.toast_invoking_share, Snackbar.LENGTH_SHORT).show();
Intent localIntent = new Intent("android.intent.action.SEND");
localIntent.setType("text/plain");
localIntent.putExtra("android.intent.extra.TEXT", mBean.title +
Expand All @@ -161,6 +164,24 @@ private void shareArticle(){
startActivity(Intent.createChooser(localIntent, getString(R.string.title_chooser_share)));
}

//

/**
* 新增 从网页上传递图片地址数组以及网页上点击图片的位置,
* 然后传递给GalleryActivity,调用ViewPager显示图片
* zhongzilu: 2016-10-25
* @param imgSrc 网页上所有的图片地址路径数组
* @param position 网页上点击的图片位置
*/
@JavascriptInterface
public void showImages(String[] imgSrc, int position){
LogUtil.d(TAG, "showImages: imageSize==>" + imgSrc.length + "\tposition==>" + position);
Intent intent = new Intent(getActivity(), GalleryActivity.class);
intent.putExtra(GalleryActivity.EXTRA_IMAGES_LIST, imgSrc);
intent.putExtra(GalleryActivity.EXTRA_CURRENT_IMAGE_POSITION, position);
startActivity(intent);
}

@Override
public void onPause() {
super.onPause();
Expand Down
Loading

0 comments on commit 09709f9

Please sign in to comment.