本文實例為大家分享了Android WebView圖片瀏覽及縮放效果展示的具體代碼,供大家參考,具體內(nèi)容如下
創(chuàng)新互聯(lián)建站是一家專注于成都網(wǎng)站設(shè)計、成都網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)與策劃設(shè)計,撫州網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)建站做網(wǎng)站,專注于網(wǎng)站建設(shè)10余年,網(wǎng)設(shè)計領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:撫州等地區(qū)。撫州做網(wǎng)站價格咨詢:13518219792
此工程用到了兩個開源庫:
PhotoView支持圖片的縮放
Android-Universal-Image-Loader圖片的異步加載
(android studio)將兩個源工程中的library文件夾導(dǎo)入到Demo Module所在的Project中,修改各自的build.gradle文件,讓里面的版本號、所用的android包等與Demo Module相同即可。大致如圖:
源代碼:
主Activity.java
public class MainActivity extends Activity { private WebView webview; private WebSettings wv; private ArrayListlistimg; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); webview = (WebView)findViewById(R.id.webview); listimg = new ArrayList<>(); wv = webview.getSettings(); wv.setJavaScriptEnabled(true); webview.addJavascriptInterface(new JavascriptInterface(this), "imagelistner");//綁定javasrcipt接口,imagelistner為接口的別名 webview.loadUrl("http://portal.smu.edu.cn/e/action/ShowInfo2.php?classid=1415&id=63663&tlpid=98"); webview.setWebViewClient(new WebViewClient(){ public boolean shouldOverrideUrlLoading(WebView view, String url) { //點擊webView中的鍵接,依然在此webview中顯示,而不跳轉(zhuǎn)到別的瀏覽器 webview.loadUrl(url); return super.shouldOverrideUrlLoading(view, url); } @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); addImageListner(); //當(dāng)頁面加載完成,就調(diào)用我們的addImageListener()函數(shù) } @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { super.onPageStarted(view, url, favicon); } }); } private void addImageListner() { //遍歷頁面中所有img的節(jié)點,因為節(jié)點里面的圖片的url即objs[i].src,保存所有圖片的src. //為每個圖片設(shè)置點擊事件,objs[i].onclick webview.loadUrl("javascript:(function(){" + "var objs = document.getElementsByTagName(\"img\"); " + "for(var i=0;i 中 listimg.add(img); } @android.webkit.JavascriptInterface //對于targetSdkVersion>=17的,要加這個聲明 public void openImage(String clickimg)//點擊圖片所調(diào)用到的函數(shù) { int index = 0; for(String url:listimg) if(url.equals(clickimg)) index = listimg.indexOf(clickimg);//獲取點擊圖片在整個頁面圖片中的位置 Intent intent = new Intent(); Bundle bundle = new Bundle(); bundle.putStringArrayList("list_image",listimg); bundle.putInt("index", index); intent.putExtra("bundle", bundle);//將所有圖片的url以及點擊圖片的位置作為參數(shù)傳給啟動的activity intent.setClass(context, ViewPagerActivity.class); context.startActivity(intent);//啟動ViewPagerActivity,用于顯示圖片 } } }
ViewPagerActivity.java
public class ViewPagerActivity extends Activity { private ViewPager mViewPager; private View rootView; private PhotoView image; private TextView indicator; private Bundle bundle; ArrayListlistimg; private int index; private int count; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_view_pager); mViewPager = (HackyViewPager) findViewById(R.id.view_pager); setContentView(mViewPager); bundle = getIntent().getBundleExtra("bundle"); listimg = bundle.getStringArrayList("list_image"); index = bundle.getInt("index"); count = listimg.size(); mViewPager.setAdapter(new SamplePagerAdapter()); mViewPager.setCurrentItem(index); } class SamplePagerAdapter extends PagerAdapter { @Override public int getCount() { return count; } @Override //此處用到PhotoView、ImageLoader均為第三方類庫的類 public View instantiateItem(ViewGroup container, int position) { rootView = View.inflate(ViewPagerActivity.this, R.layout.viewpager_item, null); image = (PhotoView) rootView.findViewById(R.id.photoview); indicator = (TextView) rootView.findViewById(R.id.indicator); ImageLoader imageloader=ImageLoader.getInstance(); imageloader.init(ImageLoaderConfiguration.createDefault(ViewPagerActivity.this)); imageloader.displayImage(listimg.get(position), image); CharSequence text = getString(R.string.viewpager_indicator, position+1, count); indicator.setText(text); container.addView(rootView, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); return rootView; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View) object); } @Override public boolean isViewFromObject(View view, Object object) { return view == object; } } }
HackViewPager.java
public class HackyViewPager extends ViewPager { private boolean isLocked; public HackyViewPager(Context context) { super(context); isLocked = false; } public HackyViewPager(Context context, AttributeSet attrs) { super(context, attrs); isLocked = false; } @Override public boolean onInterceptTouchEvent(MotionEvent ev) {//用于處理Touch事件 if (!isLocked) { try { return super.onInterceptTouchEvent(ev); } catch (IllegalArgumentException e) { e.printStackTrace(); return false; } } return false; } @Override public boolean onTouchEvent(MotionEvent event) { return !isLocked && super.onTouchEvent(event); } }
相關(guān)的布局文件:
MainActivity布局中只有一個簡單的webView;
ViewPagerActivity布局中也只是一個HackViewPager;
HackViewPager中每一個View的布局:
viewpager_item.xml
<?xml version="1.0" encoding="utf-8"?>
用到了一個@string/viewpager_indicator為
%1$d/%2$d
總結(jié):主要難點還是在獲取頁面中圖片的url,用到的是java函數(shù)與javascript的通信。