Android仿人人客户端(v5(1)

67 阅读4分钟
        @Override

        public void onSuccess(Object obj) {

            @SuppressWarnings("unchecked")

            LinkedList<FreshNews> freshNewsList = (LinkedList<FreshNews>) obj;

            if (freshNewsList.isEmpty()) {

                return;

            }



            mFreshNewsList.addAll(freshNewsList);



            mHandler.post(new Runnable() {



                @Override

                public void run() {

                    mLoadingView.setVisibility(View.GONE);

                    mFreshNewsAdapter.notifyDataSetChanged();

                }

            });



        }



        @Override

        public void onFail(int errorCode) {

            LogUtil.i(TAG, "freshNewsList errorCode = " + errorCode);



        }

    });



    mDefaultThreadPool.execute(asyncHttpsPost);

    mAsyncRequests.add(asyncHttpsPost);



}

         关于分享的图片显示效果,人人官方客户端截图如下:



![](https://p3-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/2d2afc66905c4c2e95e601d16ff318f6~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg5L2c6ICFdmlwMTAyNGPkuqTmtYE=:q75.awebp?rk3s=f64ab15b&x-expires=1771635217&x-signature=4nzUowWEDY8JN9IGD2FIxkRCGJo%3D)



        我猜测人人官方,用于显示分享的图片ImageView配置如下:



          <ImageView

                android:id="@+id/iv_photo_image"

                android:layout_width="150dip"

                android:layout_height="150dip"

                android:layout_marginTop="10dip"

                android:scaleType="centerCrop" />

  解释: 人人官方的做法,显示固定大小的图片,根据设置的宽高选择中间的区域对原图进行剪裁。



   我觉得裁剪后的图片看起来不舒服,我实现的效果图如下:



![](https://p3-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/303ec6d728d34688b64b9c6da5fd5512~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg5L2c6ICFdmlwMTAyNGPkuqTmtYE=:q75.awebp?rk3s=f64ab15b&x-expires=1771635217&x-signature=8NE0bQg8ekf8aw4Ghn8QGKkxitU%3D)  



        用于显示分享的图片ImageView配置如下:



   <ImageView

        android:id="@+id/iv_photo_image"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_marginTop="10dip" />



       完整的用于展示分享的照片的布局文件fresh\_news\_item\_share\_photo.xml如下:



<LinearLayout xmlns:android="schemas.android.com/apk/res/and…"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:orientation="horizontal" >



<View

    android:id="@+id/v_photo_left_line"

    android:layout_width="2dip"

    android:layout_height="fill_parent"

    android:background="#20333333" />



<LinearLayout

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:layout_marginLeft="10dip"

    android:orientation="vertical" >



    <TextView

        android:id="@+id/tv_photo_owner_name"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:textColor="#ff005092"

        android:textSize="14sp" />



    <TextView

        android:id="@+id/tv_photo_describe"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_marginTop="10dip"

        android:textColor="#000000"

        android:textSize="13sp" />



    <ImageView

        android:id="@+id/iv_photo_image"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_marginTop="10dip" />



   <!--  人人官方的做法,显示固定大小的图片,并对原图进行剪裁。根据设置的宽高选择适中的区域进行裁剪--> 

    <!--

            <ImageView

                android:id="@+id/iv_photo_image"

                android:layout_width="150dip"

                android:layout_height="150dip"

                android:layout_marginTop="10dip"

                android:scaleType="centerCrop" />

    -->



    <TextView

        android:id="@+id/tv_photo_source"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_marginTop="10dip"

        android:textColor="#ff888888"

        android:textSize="13sp" />

</LinearLayout>

      在fresh\_news\_list\_item.xml布局文件中,添加用于展示分享的照片方式:



    <!-- 分享的图片-->



    <include

        android:id="@+id/ll_share_photo"

        android:layout_alignLeft="@+id/tv_nickname"

        android:layout_below="@+id/tv_message_content"

        android:layout_marginTop="10dip"

        layout="@layout/fresh_news_item_share_photo"

        android:visibility="gone" />

        新鲜事列表数据适配器(FreshNewsAdapter)文件中,添加的处理: 



    case 32: // 分享照片的新鲜事。 

    case 33: // 分享相册的新鲜事。  

        

        // 设置分享标识图标

        holder.text3.setCompoundDrawablesWithIntrinsicBounds(R.drawable.v5_0_1_newsfeed_share_icon, 0, 0, 0);



          // 内容的前缀

          String prefix1 = freshNews.getPrefix();

          if (!TextUtils.isEmpty(prefix1)) {

              holder.text2.setVisibility(View.VISIBLE);

              holder.text2.setText(prefix1);

          } else {

              holder.text2.setVisibility(View.GONE);

          }

          

        break;



   在新鲜事中包含的媒体内容中,添加的条件判断分支:



            else if ("photo".equals(media_type)) {

                holder.linearLayout3.setVisibility(View.VISIBLE);



                ImageInfo imgInfo = new ImageInfo(holder.imageView2, att.getRaw_src());

                mImageLoader.displayImage(imgInfo);



                String owner_name = att.getOwner_name();

                if(!TextUtils.isEmpty(owner_name)){

                    holder.text7.setVisibility(View.VISIBLE);

                    holder.text7.setText(owner_name);

                } else {

                    holder.text7.setVisibility(View.GONE);

                }

                

                String description = freshNews.getDescription();

                if(!TextUtils.isEmpty(description)){

                    holder.text8.setVisibility(View.VISIBLE);

                    holder.text8.setText(description);

                } else {

                    holder.text8.setVisibility(View.GONE);

                }

                

                holder.text9.setText("【" + freshNews.getTitle() + "】");



            } 

    FreshNewsAdapter文件到目前为止的完整代码如下:



package com.everyone.android.ui.freshnews;

import java.util.LinkedList;

import android.graphics.Color;

import android.text.TextUtils;

import android.util.TypedValue;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.view.ViewGroup.LayoutParams;

import android.widget.BaseAdapter;

import android.widget.ImageView;

import android.widget.LinearLayout;

import android.widget.TextView;

import com.everyone.android.R;

import com.everyone.android.bitmap.ImageLoader;

import com.everyone.android.entity.Attachment;

import com.everyone.android.entity.Comment;

import com.everyone.android.entity.Comments;

import com.everyone.android.entity.FreshNews;

import com.everyone.android.entity.ImageInfo;

import com.everyone.android.entity.Source;

import com.everyone.android.ui.EveryoneActivity;

import com.everyone.android.utils.DensityUtil;

import com.everyone.android.utils.LogUtil;

/**

  • 功能描述:新鲜事列表数据适配器

  • @author android_ls

*/

public class FreshNewsAdapter extends BaseAdapter {

/**

 * LOG打印标签

 */

private static final String TAG = "FreshNewsAdapter";



private LayoutInflater inflater;



private LinkedList<FreshNews> mFreshNewsList;



private EveryoneActivity mActivity;



private ImageLoader mImageLoader;



public FreshNewsAdapter(EveryoneActivity activity, LinkedList<FreshNews> freshNewsList) {

    inflater = LayoutInflater.from(activity);

    mActivity = activity;

    mFreshNewsList = freshNewsList;

    this.mImageLoader = new ImageLoader(mActivity);

}



@Override

public int getCount() {

    return mFreshNewsList.size();

}



@Override

public Object getItem(int arg0) {

    return mFreshNewsList.get(arg0);

}



@Override

public long getItemId(int position) {

    return position;

}



/* (non-Javadoc)

 * @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup)

 */

@Override

public View getView(final int position, View convertView, ViewGroup parent) {

    ViewHolder holder = null;

    if (convertView == null) {

        convertView = inflater.inflate(R.layout.fresh_news_list_item, null);

        holder = new ViewHolder();

        holder.imageView1 = (ImageView) convertView.findViewById(R.id.iv_user_image);

        holder.text1 = (TextView) convertView.findViewById(R.id.tv_nickname);

        holder.text2 = (TextView) convertView.findViewById(R.id.tv_message_content);



        holder.linearLayout1 = (LinearLayout) convertView.findViewById(R.id.ll_comments_content);

        holder.linearLayout2 = (LinearLayout) convertView.findViewById(R.id.ll_update_status);

        holder.linearLayout3 = (LinearLayout) convertView.findViewById(R.id.ll_share_photo);



        holder.text7 = (TextView) convertView.findViewById(R.id.tv_photo_owner_name);

        holder.text8 = (TextView) convertView.findViewById(R.id.tv_photo_describe);

        holder.imageView2 = (ImageView) convertView.findViewById(R.id.iv_photo_image);

        holder.text9 = (TextView) convertView.findViewById(R.id.tv_photo_source);



        holder.text3 = (TextView) convertView.findViewById(R.id.tv_published);

        holder.text4 = (TextView) convertView.findViewById(R.id.tv_source);



        holder.text5 = (TextView) convertView.findViewById(R.id.tv_status_name);

        holder.text6 = (TextView) convertView.findViewById(R.id.tv_status_content);



        convertView.setTag(holder);

    } else {

        holder = (ViewHolder) convertView.getTag();

    }



    final FreshNews freshNews = mFreshNewsList.get(position);



    // 姓名

    holder.text1.setText(freshNews.getName());



    // 加载图像

    String headurl = freshNews.getHeadurl();

    LogUtil.i(TAG, "headurl = " + headurl);

    if (!TextUtils.isEmpty(headurl)) {

        int widthPx = DensityUtil.dip2px(mActivity, 43);

        ImageInfo imgInfo = new ImageInfo(holder.imageView1, headurl, widthPx, widthPx);

        mImageLoader.displayImage(imgInfo);

    }



    LogUtil.i(TAG, "description = " + freshNews.getDescription());

    LogUtil.i(TAG, "freshNews.getMessage() = " + freshNews.getMessage());

    LogUtil.i(TAG, "freshNews.getTitle() = " + freshNews.getTitle());

    LogUtil.i(TAG, "freshNews.getPrefix() = " + freshNews.getPrefix());



    // 用户自定义输入的内容

    String message = freshNews.getMessage();

    if (!TextUtils.isEmpty(message)) {

        holder.text2.setVisibility(View.VISIBLE);

        holder.text2.setText(message);

    } else {

        holder.text2.setVisibility(View.GONE);

    }



    // page代表公共主页新鲜事

    int feedType = freshNews.getFeed_type();

    LogUtil.i(TAG, "feedType = " + feedType);



    switch (feedType) {

    case 10: // 更新状态的新鲜事。 

    case 11: // page更新状态的新鲜事。 



        // 设置状态标识图标

        holder.text3.setCompoundDrawablesWithIntrinsicBounds(R.drawable.v5_0_1_newsfeed_status_icon, 0, 0, 0);



        // 内容的前缀

        String prefix = freshNews.getPrefix();

        if (!TextUtils.isEmpty(prefix)) {

            holder.text2.setVisibility(View.VISIBLE);

            holder.text2.setText(prefix);

        } else {

            holder.text2.setVisibility(View.GONE);

        }



        break;

    case 20: // 发表日志的新鲜事。 

    case 22: // page发表日志的新鲜事。 

        

        break;

    case 21: // 分享日志的新鲜事。 

    case 23: // page分享日志的新鲜事。 



        break;

    case 30: // 上传照片的新鲜事。

    case 31: // page上传照片的新鲜事。  

        

        break;

    case 32: // 分享照片的新鲜事。 

    case 33: // 分享相册的新鲜事。  

        

        // 设置分享标识图标

        holder.text3.setCompoundDrawablesWithIntrinsicBounds(R.drawable.v5_0_1_newsfeed_share_icon, 0, 0, 0);



          // 内容的前缀

          String prefix1 = freshNews.getPrefix();

          if (!TextUtils.isEmpty(prefix1)) {

              holder.text2.setVisibility(View.VISIBLE);

              holder.text2.setText(prefix1);

          } else {

              holder.text2.setVisibility(View.GONE);

          }

          

        break;

    // ...

    default:

        break;

    }

    

    holder.linearLayout2.setVisibility(View.GONE);

    holder.linearLayout3.setVisibility(View.GONE);

    

    // 新鲜事中包含的媒体内容

    LinkedList<Attachment> attachments = freshNews.getAttachment();

    if (attachments != null) {

        int size = attachments.size();

        LogUtil.i(TAG, "size = " + size);



        for (int i = 0; i < size; i++) {

            Attachment att = attachments.get(i);

            String media_type = att.getMedia_type();

            LogUtil.i(TAG, "media_type = " + media_type);

            LogUtil.i(TAG, "att.getContent() = " + att.getContent());

            LogUtil.i(TAG, "getHref = " + att.getHref());

            LogUtil.i(TAG, "att.getOwner_id() = " + att.getOwner_id());

            LogUtil.i(TAG, "getOwner_name() = " + att.getOwner_name());

            LogUtil.i(TAG, "getRaw_src() = " + att.getRaw_src());

            LogUtil.i(TAG, "getScr() = " + att.getScr());



            if ("status".equals(media_type)) {

                holder.linearLayout2.setVisibility(View.VISIBLE);



                holder.text5.setText(att.getOwner_name());

                holder.text6.setText(att.getContent());

            } else if ("photo".equals(media_type)) {

                holder.linearLayout3.setVisibility(View.VISIBLE);



                ImageInfo imgInfo = new ImageInfo(holder.imageView2, att.getRaw_src());

                mImageLoader.displayImage(imgInfo);



                String owner_name = att.getOwner_name();

                if(!TextUtils.isEmpty(owner_name)){

                    holder.text7.setVisibility(View.VISIBLE);

                    holder.text7.setText(owner_name);

                } else {

                    holder.text7.setVisibility(View.GONE);

                }

                

                String description = freshNews.getDescription();

                if(!TextUtils.isEmpty(description)){

                    holder.text8.setVisibility(View.VISIBLE);

                    holder.text8.setText(description);

                } else {

                    holder.text8.setVisibility(View.GONE);

                }

                

                holder.text9.setText("【" + freshNews.getTitle() + "】");



            } else if ("link".equals(media_type)) {

                

            } else if ("album".equals(media_type)) {

                

            } else if ("link".equals(media_type)) {

                

            } else if ("video".equals(media_type)) {



            } else if ("audio".equals(media_type)) {



            }



        }

    }

    

    // 动态生成显示评论信息的Item

    Comments comments = freshNews.getComments();

    if (comments != null) {

        LinkedList<Comment> commentList = comments.getComment();

        if (commentList != null) {

            holder.linearLayout1.setVisibility(View.VISIBLE);



            if (holder.linearLayout1.getChildCount() > 0) {

                holder.linearLayout1.removeAllViews();

            }



            int count = comments.getCount();

            if (count > 0) {

                TextView tvCount = new TextView(mActivity);

                tvCount.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

                tvCount.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);

                tvCount.setSingleLine();

                tvCount.setCompoundDrawablePadding(5);

                tvCount.setPadding(0, 10, 0, 0);

                tvCount.setText(count + "条评论");

                tvCount.setTextColor(Color.parseColor("#ff005092"));

                tvCount.setCompoundDrawablesWithIntrinsicBounds(R.drawable.fresh_news_comment_icon, 0, 0, 0);

                holder.linearLayout1.addView(tvCount);

            }



            int size = commentList.size();

            LogUtil.i(TAG, "commentList size = " + size);

            for (int i = 0; i < size; i++) {

                Comment comment = commentList.get(i);

                LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);



                TextView tvContent = new TextView(mActivity);

                tvContent.setLayoutParams(layoutParams);

                tvContent.setTextColor(Color.BLACK);

                tvContent.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);

                tvContent.setSingleLine();

                tvContent.setPadding(0, 10, 0, 0);

                tvContent.setText(comment.getName() + ":" + comment.getText());

                holder.linearLayout1.addView(tvContent);



                TextView tvTime = new TextView(mActivity);

                tvTime.setTextColor(Color.GRAY);

                tvTime.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);

                tvTime.setLayoutParams(layoutParams);

                tvContent.setPadding(0, 5, 0, 10);

                tvTime.setText(comment.getTime());

                holder.linearLayout1.addView(tvTime);

            }



        } else {

            holder.linearLayout1.setVisibility(View.GONE);

        }

    } else {

        holder.linearLayout1.setVisibility(View.GONE);

    }



    // 对获取的时间字符串的处理

    String updateTime = freshNews.getUpdate_time();

    if (!TextUtils.isEmpty(updateTime)) {

        updateTime = updateTime.substring(updateTime.indexOf("-") + 1, updateTime.lastIndexOf(":"));

        updateTime = updateTime.replace("-", "月");

        updateTime = updateTime.replace(" ", "日 ");

        int index = updateTime.indexOf("0");

        if (index == 0) {

            updateTime = updateTime.substring(index + 1);

        }



        holder.text3.setText(updateTime);

    }



    // 来自那种客户端

    Source source = freshNews.getSource();

    if (source != null) {

        holder.text4.setText("来自:" + source.getText());

    }



    return convertView;

}



static class ViewHolder {



    public LinearLayout linearLayout1;



    public LinearLayout linearLayout2;



    public LinearLayout linearLayout3;



    public ImageView imageView1;



    public ImageView imageView2;



    public TextView text1;



    public TextView text2;



    public TextView text3;



    public TextView text4;



    public TextView text5;



    public TextView text6;



    public TextView text7;



    public TextView text8;



    public TextView text9;

}

}


三、对从网络(人人的服务器端)获取的图片,本地处理部分 修改 根据指定的压缩比例,获得合适的Bitmap这一步。



   之前的处理代码如下:



/**

 * 根据指定的压缩比例,获得合适的Bitmap

 * @param inStream InputStream

 * @param width 指定的宽度

 * @param height 指定的高度

 * @return Bitmap

 * @throws IOException

 */

public static Bitmap decodeStream(InputStream inStream, int width, int height) throws IOException {

    // 从输入流读取数据

    byte[] data = StreamTool.read(inStream);



    BitmapFactory.Options options = new BitmapFactory.Options();

    options.inJustDecodeBounds = true;

    BitmapFactory.decodeByteArray(data, 0, data.length, options);



    int w = options.outWidth;

    int h = options.outHeight;

    

    // 缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可

    int ratio = 1; // 默认为不缩放

    if (w >= h && w > width) {

        ratio = (int) (w / width);

    } else if (w < h && h > height) {

        ratio = (int) (h / height);

    }



    if (ratio <= 0) {

        ratio = 1;

    }



    System.out.println("图片的缩放比例值ratio = " + ratio);



    options = new BitmapFactory.Options();

    // 属性值inSampleSize表示缩略图大小为原始图片大小的几分之一,即如果这个值为2,

    // 则取出的缩略图的宽和高都是原始图片的1/2,图片大小就为原始大小的1/4。

    options.inSampleSize = ratio;



    options.inJustDecodeBounds = false;

    return BitmapFactory.decodeByteArray(data, 0, data.length, options);

}

   仔细阅读上面的代码,会发现其只满足用于显示固定大小图片的处理。可是在实际需求中往往有,我不知道服务器端返回的图片是什么样的(主要指尺寸大小、长宽比),但是我要求不管服务器端返回的图片是什么样,客户端都能“正确”的显示。这时就需要有一种算法,能根据服务器端返回的图片大小自动计算缩放比,以便我们获取合适的图片。



   修改后的处理代码如下:



/**

 * 根据指定的压缩比例,获得合适的Bitmap

 * @param inStream InputStream

 * @param width 指定的宽度

 * @param height 指定的高度

 * @return Bitmap

 * @throws IOException

 */

public static Bitmap decodeStream(InputStream inStream, int width, int height) throws IOException {

    // 从输入流读取数据

    byte[] data = StreamTool.read(inStream);



    BitmapFactory.Options options = new BitmapFactory.Options();

    options.inJustDecodeBounds = true;

    BitmapFactory.decodeByteArray(data, 0, data.length, options);



    /*计算缩放比

      属性值inSampleSize表示缩略图大小为原始图片大小的几分之一,即如果这个值为2,

      则取出的缩略图的宽和高都是原始图片的1/2,图片大小就为原始大小的1/4。*/                

    if (width == 0 && height == 0) {

        options.inSampleSize = calculateInSampleSize(options);

    } else {

        options.inSampleSize = calculateInSampleSize(options, width, height);

    }



    options.inJustDecodeBounds = false;

    return BitmapFactory.decodeByteArray(data, 0, data.length, options);

}



/**

 * 根据图片大小自动计算缩放比

 * 图片的宽度或高度有一个值最大不能大于defaultSize变量值的两倍