Android的画廊与第一缩略图的封页 [英] Android Gallery with first thumbnail as coverpage

查看:245
本文介绍了Android的画廊与第一缩略图的封页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢您阅读!

一些背景资料:

我建立从教程这里图库应用

只改变我这个code做是为了取代

Only change I made to this code is to replace


i.setLayoutParams(新Gallery.LayoutParams(150,100));


i.setLayoutParams(新Gallery.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));

只显示一次(有点像一个幻灯片浏览器)图库图片。

to display only one gallery image at a time (Kinda like a slideshow viewer).

问题:

我想画廊的第一个缩略图作为专辑封面的行为有两个的TextView 的显示专辑信息。

I want the first thumbnail of the gallery to act as an Album Cover with two TextView's to display Album info.

实验:
所以,我创建了一个 cover.xml 是这样的:

 
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:padding="6dip">
    <ImageView android:id="@+cover/imgImage" android:adjustViewBounds="true"
        android:layout_width="fill_parent" android:layout_height="fill_parent">
    </ImageView>
    <TextView android:id="@+cover/tvCoverText1"
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:maxLines="2" android:text="Text1" />
    <TextView android:id="@+cover/tvCoverText2"
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:singleLine="true" android:maxLines="1" android:layout_below="@cover/tvCoverText1"
        android:text="Text2" />
</RelativeLayout>

和这里的Java的code。我检查 getView()如果位置是0(第一个缩略图),然后玩的意见。

And here's the Java code. I check in getView() if the position is 0 (the first thumbnail) and then play around with the views.


package com.sagar.sample;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class Main extends Activity {

    private LayoutInflater mInflater = null;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Gallery g = (Gallery) findViewById(R.main.gallery);
        g.setAdapter(new ImageAdapter(this));

        g.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView parent, View v, int position, long id) {
                Toast.makeText(Main.this, "" + position, Toast.LENGTH_SHORT).show();
            }
        });
    }

    public class ImageAdapter extends BaseAdapter {
        int mGalleryItemBackground;
        private Context mContext;

        private Integer[] mImageIds = {
                0, R.drawable.bp1, R.drawable.bp2, R.drawable.bp3 
        };

        public ImageAdapter(Context c) {
            mContext = c;
            mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//            TypedArray a = obtainStyledAttributes(R.styleable.HelloGallery);
//            mGalleryItemBackground = a.getResourceId(
//                    R.styleable.HelloGallery_android_galleryItemBackground, 0);
//            a.recycle();
        }

        public int getCount() {
            return mImageIds.length;
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            View view = convertView;
            if(view == null) {
                view = mInflater.inflate(R.layout.cover, null);

                viewHolder = new ViewHolder();
                viewHolder.tvCoverText1 = (TextView)view.findViewById(R.cover.tvCoverText1);
                viewHolder.tvCoverText2 = (TextView)view.findViewById(R.cover.tvCoverText2);
                viewHolder.imgView = (ImageView)view.findViewById(R.cover.imgImage);
                view.setTag(viewHolder);             
            }
            else {
                viewHolder = (ViewHolder)view.getTag();
            }

            if(position == 0) {
                viewHolder.tvCoverText1.setVisibility(View.VISIBLE);
                viewHolder.tvCoverText2.setVisibility(View.VISIBLE);
                viewHolder.imgView.setVisibility(View.GONE);
            }
            else {
                viewHolder.tvCoverText1.setVisibility(View.GONE);
                viewHolder.tvCoverText2.setVisibility(View.GONE);
                viewHolder.imgView.setVisibility(View.VISIBLE);
                //viewHolder.imgView = new ImageView(mContext);
                viewHolder.imgView.setImageResource(mImageIds[position]); //Album cover is at 0th position
                viewHolder.imgView.setLayoutParams(new Gallery.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
                viewHolder.imgView.setScaleType(ImageView.ScaleType.FIT_XY);
                viewHolder.imgView.setBackgroundResource(mGalleryItemBackground);
            }
            return view;
        }
    }

    static class ViewHolder {
        TextView tvCoverText1, tvCoverText2;
        ImageView imgView;
    }
}

最终结果是:
当应用程序加载时,我第一次看到空白屏幕一段时间,那么该视图改为显示AlbumCover。而且它是痛苦的缓慢穿过图像中滚动。

End Result: When the app loads up, I first see a blank screen for a while and then the view changes to display the AlbumCover. And it's painfully slow to scroll across the images.

Hmm..obviously,我做错了什么。我真诚地希望有人能帮助我在这里:(

Hmm..obviously, I am doing something wrong. I sincerely hope someone could help me here :(

谢谢!

更新:添加的main.xml


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent">
    <Gallery
        android:id="@+main/gallery"
        android:layout_width="fill_parent" android:layout_height="fill_parent"
    />
</RelativeLayout>

UPDATE2:那么,这里的一些伪code解释什么,我想实现:

UPDATE2: So, here's some psuedo code to explain what I am trying to achieve:


if(position == 0)
    //show toptext and bottomtext from cover.xml
else
    //show tvTitle1 and tvTitle2 (may later include tvTitle3 and tvTitle4) from main.xml

现在,只有位置0的情况下工作,而且也当我刷到位置1和轻扫回到位置0 - 在的TextView s的变灰,隐约可见。 (

Right now, only the position 0 case works and that too when I swipe to position 1 and swipe back to position 0 - the TextViews are grayed out and barely visible. :(

推荐答案

想在这里提出的所有方法后,我没能仍然得到一个定制的画廊我想它的方式。我一直运行到 ClassCastException异常。因此,<一个href=\"http://stackoverflow.com/questions/5670488/switch-between-gallery-and-a-linearlayout-classcastexception\">here's什么为我工作至今。这只是一种变通方法和柜面有人想出了一个更好的办法来设计定制的画廊 - 不要在这里发表您的答案,所以我可以接受。

After trying all approaches suggested here, I wasn't able to still get a custom gallery the way I wanted it to. I kept running into ClassCastException. So, here's what worked for me till now. This is just a workaround and incase someone comes up with a better way to design a custom gallery - do post your answer here so I can accept it.

感谢您的帮助了!

这篇关于Android的画廊与第一缩略图的封页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆