模仿网易新闻图片点击放大效果

先把效果拿出来看一下,点击新闻图片可以弹出一个DIALOG放大


原理如下:
服务端需要处理:
1、定义一个新闻浏览页面,如news_list.php?newsid=1
把新闻浏览的内容读取出来

需要注意的是内容里的图片,需要给图片增加一个事件onClick=\"javascript:contactsAction.setImgSrc('$url')\" 



$code = "<img src=\"$turl\" border=\"0\"  onClick=\"javascript:contactsAction.setImgSrc('$url')\"  style=\"$styleCss\" onload=\"$onload\" $alt>";


客户端处理

1、bbsinfo_listhtml.xml 新闻内容显示模版
2、bbsimgshow.xml 新闻图片点击放大效果
3、BbsinfoList.java 新闻内容读取页面
4、BbsImageShow.java 图片放大页面


处理过程:
新闻内容页放置webview控件
当内容显示出来时,点击图片触发服务端的JS事件,客户端抓取服务端返回的数据,然后打开图片放大窗体,进行显示。再点击一次就关闭窗体。
1、bbsinfo_listhtml.xml代码
  <WebView
            android:id="@+id/webView1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />

2、bbsimgshow.xml代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
  
<ImageView android:id="@+id/ivbig"
        android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>
</LinearLayout>

3、BbsinfoList.java

package com.wyf.tab;

import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.wyf.tab.util.Constant;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebView.HitTestResult;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

public class BbsinfoList extends Activity implements OnClickListener {
        private WebView wv;
        String tid;
        private String url;

        LinearLayout ld;
        private ImageView bt_left;
        Intent intent = new Intent();

        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.bbsinfo_listhtml);
                // 加载动画
                ld = (LinearLayout) findViewById(R.id.loading);
                ld.setVisibility(View.VISIBLE);
                findview();
                //
                Thread thread = new Thread(new Runnable() {
                        public void run() {
                                Message message = new Message();
                                message.what = 0;
                                mHandler.sendMessage(message);
                        }
                });
                thread.start();
                // 获取传过来的参数
                Bundle bundle = this.getIntent().getExtras();
                tid = bundle.getString("tid");
                url = Constant.WEB_DIR + Constant.ThreadInfoUrl_webview + tid;

        }

        // 处理跳转到主Activity
        private Handler mHandler = new Handler() {
                @Override
                public void handleMessage(Message msg) {
                        Log.d(">>>>>Mhandler", "开始handleMessage");
                        showContent(url);
                        Log.d(">>>>>Mhandler", "LoadActivity关闭");
                        if (msg.what == 0) {
                                ld.setVisibility(View.GONE);
                        }
                }
        };

        private void findview() {

                wv = (WebView) findViewById(R.id.webView1);
                wv.setVisibility(WebView.VISIBLE);
                WebSettings ws = wv.getSettings();
                ws.setUseWideViewPort(true);
                ws.setJavaScriptEnabled(true);
                wv.addJavascriptInterface(new ContactsPlugin(), "contactsAction");
                ws.setSupportZoom(true);
                wv.setWebChromeClient(new WebChromeClient() {

                        public void onProgressChanged(WebView view, int progress) {
                                setTitle("页面加载中,请稍候..." + progress + "%");
                                setProgress(progress * 100);
                                ld.setVisibility(View.VISIBLE);
                                if (progress == 100) {
                                        setTitle(R.string.app_name);
                                        ld.setVisibility(View.GONE);
                                }
                        }
                });
                //
                bt_left = (ImageView) findViewById(R.id.btn_left);
                bt_left.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {
                                finish();
                        }
                });
        }

        private void showContent(String url) {
                wv.loadUrl(url);
        }

        @Override
        public void onClick(View v) {
                // TODO Auto-generated method stub

        }
        
        private class ContactsPlugin {            
            /**
             * 拨号
             */
            @SuppressWarnings("unused")
                public void setImgSrc(String imgSrc) {
                    //Toast.makeText(BbsinfoList.this, imgSrc + "数据加载完!",
                        //                Toast.LENGTH_LONG).show();
                        Log.e("imgurl", imgSrc);
                        Intent intent = new Intent(BbsinfoList.this, BbsImageShow.class);
                        Bundle bundle = new Bundle();
                        bundle.putString("imgSrc", imgSrc);
                        intent.putExtras(bundle);
                        startActivity(intent);
            }
    }

}

4、BbsImageShow.java

package com.wyf.tab;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.Toast;
public class BbsImageShow  extends Activity{
        private ImageView iv;

        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                requestWindowFeature(Window.FEATURE_NO_TITLE);
                setContentView(R.layout.bbsimgshow);

                findview();
        }

        /*
         * 初始化控件,加载数据
         */
        private void findview() {
                Intent intent = this.getIntent();
                Bundle bundle = intent.getExtras();
                String imgSrc = bundle.getString("imgSrc");
                //Bitmap bm = BitmapFactory.decodeFile(imgSrc);
                Bitmap bm = returnBitMap(imgSrc); 
                iv = (ImageView) findViewById(R.id.ivbig);
                iv.setImageBitmap(bm);
                iv.setOnClickListener(new ImageView.OnClickListener() {

                        public void onClick(View arg0) {
                                //Toast.makeText(ImageShow.this, "关闭图片", Toast.LENGTH_SHORT).show();
                                finish();
                        }
                });
                

        }
         //获取图片路径函数
          public static Bitmap returnBitMap(String url) {  
                  URL myFileUrl = null;  
                  Bitmap bitmap = null;  
                  try {  
                  myFileUrl = new URL(url);  
                  } catch (MalformedURLException e) {  
                  e.printStackTrace();  
                  }  
                  try {  
                  HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();  
                  conn.setDoInput(true);  
                  conn.connect();  
                  InputStream is = conn.getInputStream();  
                  bitmap = BitmapFactory.decodeStream(is);  
                  is.close();  
                  } catch (IOException e) {  
                  e.printStackTrace();  
                  }  
                  return bitmap;  
                } 
}


你可能感兴趣的:(JavaScript,android,String,layout,url,网易)