SmartImageView

来源:互联网 发布:修改照片大小的软件 编辑:程序博客网 时间:2024/06/05 13:26

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >


    <ListView
        android:id="@+id/listView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </ListView>


</RelativeLayout>

**********************************************************************

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >


    <com.loopj.android.image.SmartImageView
        android:id="@+id/imageView"
        android:layout_width="100dp"
        android:layout_height="100dp" />


    <TextView
        android:id="@+id/textView_intro"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/imageView"
        android:layout_marginBottom="27dp"
        android:layout_toRightOf="@+id/imageView"
        android:text="fffffffffffffffffffddddddddddddd" />


    <TextView
        android:id="@+id/textView_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView_intro"
        android:layout_alignLeft="@+id/textView_intro"
        android:layout_marginBottom="16dp"
        android:text="fffffffffffffffffff" />
    
</RelativeLayout>

==================================================================================

package com.example.day_07_SmartImageView;



import java.io.IOException;
import java.util.List;


import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;


import com.example.day_07_SmartImageView.bean.Bean;
import com.example.day_07_SmartImageView.bean.NewsInfo;
import com.google.gson.Gson;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;


public class MainActivity extends Activity {
String path = "http://api.sina.cn/sinago/list.json?channel=hdpic_story&adid=4ad30dabe134695c3d7c3a65977d7e72&from=6042095012&chwm=12050_0001&imei=867064013906290&uid=802909da86d9f5fc&p=1";


private List<NewsInfo> list;
Handler handler=new Handler(){


public void handleMessage(android.os.Message msg) {

Bean bean = (Bean) msg.obj;
list = bean.data.list;

MyAdapter myAdapter=new MyAdapter(MainActivity.this,list);
listView.setAdapter(myAdapter);


};


};


private ListView listView;

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

listView = (ListView) findViewById(R.id.listView);
//点击进入详情
listView.setOnItemClickListener(new OnItemClickListener() {


@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {

list.get(position);
}
});

new Thread() {


public void run() {

sendGet();
};


}.start();


}


/**
* 请求网络
*/
protected void sendGet() {

HttpClient httpClient=new DefaultHttpClient();

HttpGet httpGet=new HttpGet(path);


try {
HttpResponse httpResponse = httpClient.execute(httpGet);

if(httpResponse.getStatusLine().getStatusCode()==200){

HttpEntity entity = httpResponse.getEntity();
String json = EntityUtils.toString(entity);




Gson gson=new Gson();
Bean bean = gson.fromJson(json, Bean.class);


Message msg=Message.obtain();
msg.obj=bean;
handler.sendMessage(msg);

}



} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}


}

=====================================================================================================

适配器

/**
 * 
 */
package com.example.day_07_SmartImageView;


import java.util.List;


import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;


import com.example.day_07_SmartImageView.bean.NewsInfo;
import com.loopj.android.image.SmartImageView;


/**
 * @author WJL
 * 
 */
public class MyAdapter extends BaseAdapter {
Context context; List<NewsInfo> list;
/**
* @param mainActivity
* @param list
*/
public MyAdapter(Context context, List<NewsInfo> list) {

this.context=context;
this.list=list;
}


/* (non-Javadoc)
* @see android.widget.Adapter#getCount()
*/
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}


/* (non-Javadoc)
* @see android.widget.Adapter#getItem(int)
*/
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}


/* (non-Javadoc)
* @see android.widget.Adapter#getItemId(int)
*/
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}


/* (non-Javadoc)
* @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup)
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {


convertView=View.inflate(context,R.layout.item, null);

SmartImageViewimageView=(SmartImageView) convertView.findViewById(R.id.imageView);
TextView textView_intro= (TextView) convertView.findViewById(R.id.textView_intro);
TextView textView_title= (TextView) convertView.findViewById(R.id.textView_title);

textView_intro.setText(list.get(position).intro);
textView_title.setText(list.get(position).title);

//使用SmartImageView展示网络图片,传入图片网络地址即可
imageView.setImageUrl(list.get(position).pic);

return convertView;
}


}

0 0