6

在此处输入图像描述

我想在我的自定义列表视图中显示动画 GIF 图像。我的 GIF 图像在 Web 服务 url 中获取。GIF 图像显示但没有动画。

public class NewsScreenAdapter extends BaseAdapter {

    LayoutInflater inflater;
    public GifDecoderView webview1;
    public static  viewholder holder;
    View view = null;
    public static Context context;
    public ImageLoader IL;
    public String imgUrl;
    public static String addurl;    
    public Activity activity;
    String image;
    public static Date parsed;
    public static String ac, cat_id;

    public NewsScreenAdapter(Activity a) {
        // TODO Auto-generated constructor stub

        context = a.getApplicationContext();
        this.activity = a;       
        inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        IL = new ImageLoader(activity.getApplicationContext());   
     }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        // return NewsScreenActivity.arrayList_header.size();

        return NewsScreenActivity.TotalDataArray.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public int getItemViewType(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public int getViewTypeCount() {
        // TODO Auto-generated method stub
        return NewsScreenActivity.TotalDataArray.size();
    }
    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        View vi = convertView;

            holder = new viewholder();

            vi = inflater.inflate(R.layout.newsscren_row, null);
            holder.news_header_title = (TextView) vi.findViewById(R.id.header_title);
            holder.ll_data = (LinearLayout) vi.findViewById(R.id.data);

            vi.setTag(holder);

        holder.news_header_title.setOnClickListener(new OnClickListener() {

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

                cat_id = NewsScreenActivity.arrayList_header.get(position);
                ac = ((NewsScreenActivity.MainData) NewsScreenActivity.TotalDataArray.get(position)).catId;

                activity.startActivity(new Intent(activity,CategoryActivity.class).putExtra("id", ac));

            }
        });

        holder.ll_data.removeAllViews();

        int storyLenght = ((NewsScreenActivity.MainData) NewsScreenActivity.TotalDataArray.get(position)).storyArr.size();

        Log.d("Adapter ", " story Lenght " + storyLenght); 

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

            view = LayoutInflater.from(activity).inflate(R.layout.sub_row, null);

            holder.short_text = (TextView) view.findViewById(R.id.short_text);
            holder.image = (ImageView) view.findViewById(R.id.image);
            holder.des = (TextView) view.findViewById(R.id.des);
            holder.date_time = (TextView) view.findViewById(R.id.date_time);
            holder.llAdd = (LinearLayout) view.findViewById(R.id.llAdd);
            holder.imgAdd = (ImageView) view.findViewById(R.id.imgAdd);

            try {
                holder.image.setTag(NewsScreenActivity.arrayList_image.get(i));
                IL.DisplayImage(
                        ((NewsScreenActivity.ImagesData) ((NewsScreenActivity.StoryData) ((NewsScreenActivity.MainData) NewsScreenActivity.TotalDataArray
                                .get(position)).storyArr.get(i)).imageArr.get(0)).smallurl, activity, holder.image);

                notifyDataSetChanged();

            } catch (Exception e) {
                // TODO: handle exception
            }

            holder.short_text.setText(((NewsScreenActivity.StoryData) ((NewsScreenActivity.MainData) NewsScreenActivity.TotalDataArray
                            .get(position)).storyArr.get(i)).title);
            holder.des.setText(((NewsScreenActivity.StoryData) ((NewsScreenActivity.MainData) NewsScreenActivity.TotalDataArray
                            .get(position)).storyArr.get(i)).description);


            String st = ((NewsScreenActivity.StoryData) ((NewsScreenActivity.MainData) NewsScreenActivity.TotalDataArray
                    .get(position)).storyArr.get(i)).date;
            parsed = new Date(Long.parseLong(st.substring(6, st.length() - 2)));

            SimpleDateFormat sdf = new SimpleDateFormat("MMM dd,yyyy hh:mmaa");
            System.out.println(sdf.format(parsed));
            String concat = sdf.format(parsed);
            String data = concat;
            String half1 = data.substring(0, 11);
            Log.e("1st date", "" + half1);

            SimpleDateFormat display_date = new SimpleDateFormat("dd.MM.yyyy");
            Date d_date = new Date();
            String dis_date = display_date.format(parsed);

            String half2 = data.substring(11, 19);
            Log.e("2st time", "" + half2);      

            SimpleDateFormat currentdate = new SimpleDateFormat("MMM dd,yyyy");
            Date currunt = new Date();
            String day = currentdate.format(currunt);
            if (half1.equalsIgnoreCase(day) == true) {
                holder.date_time.setText(half2);
                Log.v("if condition", "" + half2);
            } else {
                half1 = dis_date;
                holder.date_time.setText(half1);
                Log.v("else condition", "" + half1);
            }
            Log.e("currunt time", "" + day); 

            holder.news_header_title.setText(((NewsScreenActivity.MainData) NewsScreenActivity.TotalDataArray
                            .get(position)).catDisplay);


            if (!((NewsScreenActivity.StoryData) ((NewsScreenActivity.MainData) NewsScreenActivity.TotalDataArray
                    .get(position)).storyArr.get(i)).advertising
                    .equalsIgnoreCase("null")) {

                 holder.short_text.setVisibility(view.GONE);
                 holder.date_time.setVisibility(view.GONE);
                 holder.des.setVisibility(view.GONE);

                imgUrl = ((NewsScreenActivity.StoryData) ((NewsScreenActivity.MainData) NewsScreenActivity.TotalDataArray
                        .get(position)).storyArr.get(i)).adData.imageurl;

                // TODO Auto-generated method stub
                addurl = ((NewsScreenActivity.StoryData) ((NewsScreenActivity.MainData) NewsScreenActivity.TotalDataArray
                        .get(position)).storyArr.get(i)).adData.targeturl;


                //-----------------GIF Image view ------------


                holder.imgAdd.setImageBitmap(IL.getBitmap(imgUrl));

                /* InputStream is = null;
                    try {
                        is = (InputStream) new URL(imgUrl).getContent();

                  webview1 = new GifDecoderView(context, is);           
                  activity.setContentView(webview1);                                            

                    } catch (Exception e) {
                        return null;
                    }*/

                holder.imgAdd.setOnClickListener(new OnClickListener() {                    
                    @Override
                    public void onClick(View v) {                       

                        activity.startActivity(new Intent(activity, AdvertismentActivity.class));

                    }
                });             
                Log.i("---", "---------" + imgUrl);
                holder.llAdd.setVisibility(View.VISIBLE);
              }

            holder.ll_data.addView(view);               
            Log.i("Set Tag", position+"OK"+i);
            view.setTag(position+"OK"+i);

            view.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                    String tag = (String) v.getTag();
                    String[] arr = tag.split("OK");
                    int p = Integer.parseInt(arr[0]);
                    int i = Integer.parseInt(arr[1]);
                    Log.i("Pos and I", p + "   " + i );

                    String str = ((NewsScreenActivity.StoryData) ((NewsScreenActivity.MainData) NewsScreenActivity.TotalDataArray .get(p)).storyArr.get(i)).storyid;

                    Log.i("Pos and I and STR", p + "   " + i + "   "  + str);
                    Intent intent = new Intent(context,ShowFullDescriprion.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.putExtra("id", str);
                    intent.putExtra("cat", p);
                    intent.putExtra("pos",i);
                    context.startActivity(intent);
                }
            });
        }
        return vi;
    }

    public static String getDate(long milliSeconds, String dateFormat) {
        // Create a DateFormatter object for displaying date in specified
        // format.
        DateFormat formatter = new SimpleDateFormat(dateFormat);

        // Create a calendar object that will convert the date and time value in
        // milliseconds to date.
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(milliSeconds);
        return formatter.format(calendar.getTime());         
    }    
    public static class viewholder {

        TextView news_header_title, short_text, des, date_time;
        LinearLayout ll_data, llAdd;
        public ImageView image, imgAdd;

    }

} 
4

5 回答 5

4

您可以使用能够解码 gif 的Android Movie类。

Movie.decodeStream(InputStream is);
于 2012-10-10T13:35:08.560 回答
2

使用WebView而不是 ImageView 和loadUrlWebView。

于 2012-09-04T12:30:56.673 回答
1

您必须为此使用 gif 解码器类并将其设置在 inflact 视图中,以便您可以在单个活动或屏幕中设置两个视图。

于 2012-08-28T13:16:40.213 回答
1

使用Glide 图像加载库,它内置了对 GIF 的支持,并且语法与加载 JPEG 文件相同:

Glide.with(this).load("http://.../anim.gif").into(imageView);

有关更多信息,请参阅wiki并运行Gihpy 示例以感到惊讶 :)

于 2014-11-13T14:42:35.400 回答
0
//-----------------GIF Image view ------------

holder.imgAdd.getSettings().setJavaScriptEnabled(true);

holder.imgAdd.setWebChromeClient(new WebChromeClient() {
    public void onProgressChanged(WebView view, int progress)
    {
        activity.setTitle("Loading...");
        activity.setProgress(progress * 100);

        if(progress == 100)
            activity.setTitle(R.string.app_name);
    }
});

holder.imgAdd.setWebViewClient(new WebViewClient() {
    @Override
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
    {
        // Handle the error
    }
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url)
    {
        view.loadUrl(url);
        return true;
    }
});

            holder.imgAdd.loadUrl(imgUrl);
于 2012-10-11T07:35:38.307 回答