1

在我的应用程序中,我有一个带有四个按钮的布局,每个按钮在被调用时都应该显示一系列图像(大约 5-6 个)。我刚刚知道 BitmapFactory 并且我正在学习实现它,但我的问题是显示这些图像的最佳方式是什么?

我在想手指滑动(不是计时器)。如果我错了,请纠正我:标签仅适用于 3 或更少,图像鳍状肢 2。所以我想知道普遍的共识。

再次感谢各位大神

4

3 回答 3

0

我建议您使用带有此库的 gridview:https ://github.com/nostra13/Android-Universal-Image-Loader 。该库非常易于实现,因此您的应用程序运行更流畅,您不必担心 OutOfMemoryErrors 或位图。

GridView 教程:http ://www.androidhive.info/2012/02/android-gridview-layout-tutorial/

于 2013-09-26T02:20:20.557 回答
0

也许gridview可以。是一个例子。

您可以在按下按钮时以这种方式显示图像。

在此处输入图像描述

于 2013-09-26T02:24:43.483 回答
0

首先,在我的应用程序中,我使用查看寻呼机来显示 5 张图像。我正在使用一个数组并通过寻呼机适配器显示它。这是我的代码:

食物.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:background="#00FF00" >
<android.support.v4.view.ViewPager
    android:id="@+id/view_pager_ayam"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/desc_text"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:layout_marginLeft="@dimen/view_pager_margin_fix"
    android:layout_marginRight="@dimen/view_pager_margin_fix" >
    <android.support.v4.view.PagerTabStrip
        android:id="@+id/pager_strip"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="#33b5e5"
        android:paddingBottom="4dp"
        android:paddingTop="4dp"
        android:textColor="#fff" >
    </android.support.v4.view.PagerTabStrip>
</android.support.v4.view.ViewPager>

这些是 values/dimens.xml 中的维度:

<resources>

<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="view_pager_margin">-64dp</dimen>
<dimen name="view_pager_margin_fix">32dp</dimen>

这是 viewpager_item.xml:

<?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" >

<TextView
    android:id="@+id/description_menu"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/menu"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="description" />

<TextView
    android:id="@+id/price_menu"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/description_menu"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="price" />

<ImageView
    android:id="@+id/menu"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:background="#00FF00" />

这是 Food.java:

package com.example.app;

public class Food extends Activity{

ArrayList<Float> pos = new ArrayList<Float>();
private int [] galeri_images_ayam;
private String [] label_ayam;
private String [] price;
Boolean isScrooled = false;
ViewPager vp;
PagerTitleStrip pts;
Timer tm;
int page = 1;
PagerAdapter adapt;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.food);
    
    galeri_images_ayam = new int [] {
            0,
            R.drawable.ayam_bakar, 
            R.drawable.ayam_goreng, 
            R.drawable.ayam_kremes, 
            R.drawable.ayam_lombok_ijo, 
            R.drawable.ayam_penyet,
            0
    };
    
    label_ayam = new String [] {
            "",
            "Paket 1",
            "Paket 2",
            "Paket 3",
            "Paket 4",
            "Paket 5",
            ""
    };
    
    price = new String [] {
            "",
            "Harga Rp.8000,-",
            "Harga Rp.9000,-",
            "Harga Rp.9500,-",
            "Harga Rp.8500,-",
            "Harga Rp.7500,-",
            ""
    };
    
    vp = (ViewPager)findViewById(R.id.view_pager_ayam);
    adapt = new Makanan_Ayam_Image_Adapter(this, galeri_images_ayam, label_ayam, price);
    vp.setAdapter(adapt);                          
  vp.setPageMargin(getResources().getDimensionPixelOffset(R.dimen.view_pager_margin));
            vp.setCurrentItem(1);
    vp.setOnPageChangeListener(new OnPageChangeListener() {
        
        @Override
        public void onPageSelected(int arg0) {
            
            pos.clear();
            
            if(arg0 == 1){
                Toast.makeText(getApplicationContext(), "Paket 1", Toast.LENGTH_SHORT).show();
            } else if(arg0 == 2){
                Toast.makeText(getApplicationContext(), "Paket 2", Toast.LENGTH_SHORT).show();
            } else if(arg0 == 3){
                Toast.makeText(getApplicationContext(), "Paket 3", Toast.LENGTH_SHORT).show();
            } else if(arg0 == 4){
                Toast.makeText(getApplicationContext(), "Paket 4", Toast.LENGTH_SHORT).show();
            } else if(arg0 == 5){
                Toast.makeText(getApplicationContext(), "Paket 5", Toast.LENGTH_SHORT).show();
            }
            
        }
        
        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
            try {
            pos.add(arg1);
            if (pos.size() > 0)
                if (arg0 == galeri_images_ayam.length - 1 & pos.get(0) > pos.get(pos.size() - 1) & isScrooled == true) {
                    try {
                        isScrooled = false;
                        vp.setCurrentItem(1, false);
                    } catch (Exception e) {
                        Log.v("tag", "swipe left" + e.toString());
                    }
                } else if(arg0 == 0 & pos.get(0) < pos.get(pos.size() - 1) & isScrooled == true) {
                    try {
                        isScrooled = false;
                        vp.setCurrentItem(galeri_images_ayam.length - 1, false);
                    } catch (Exception e) {
                        Log.v("tag", "swipe right" + e.toString());
                    }
                } else if(arg0 == 0 & pos.size() == 1 & isScrooled == true){
                    try {
                        isScrooled = false;
                        vp.setCurrentItem(galeri_images_ayam.length - 1, false);
                    } catch (Exception e) {
                        Log.v("tag", "swipe right " + e.toString());
                    }
                }
            } catch (Exception e) {
                Log.v("tag", e.toString());
            }
        }
        
        @Override
        public void onPageScrollStateChanged(int arg0) {
            isScrooled = true;
        }
    });
}

public class Makanan_Ayam_Image_Adapter extends PagerAdapter{

    Context context;
    int galeri_image [];
    String lbl_ayam [];
    String price_menu [];
    LayoutInflater inflater;
    
    public Makanan_Ayam_Image_Adapter(Context context, int [] galeri_images_ayam, String [] label_ayam, String [] price) {
        this.context = context;
        this.galeri_image = galeri_images_ayam;
        this.lbl_ayam = label_ayam;
        this.price_menu = price;
    }
    
    @Override
    public int getCount() {
        return label_ayam.length;
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == ((RelativeLayout)arg1);
    }
    
    @Override
    public Object instantiateItem(ViewGroup container, final int pos){
        TextView txt_desc;
        TextView txt_price;
        ImageView imgvw;
        inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View itemView = inflater.inflate(R.layout.viewpager_item, container, false);
        txt_desc = (TextView)itemView.findViewById(R.id.description_menu);
        txt_price = (TextView)itemView.findViewById(R.id.price_menu);
        txt_desc.setText(label_ayam[pos]);
        txt_price.setText(price[pos]);
        final int current_pos = pos;
        imgvw = (ImageView)itemView.findViewById(R.id.menu); 
        int padding = context.getResources().getDimensionPixelSize(R.dimen.padding_medium);
        imgvw.setPadding(padding, padding, padding, padding);
        imgvw.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        imgvw.setImageResource(galeri_images_ayam[pos]);
        ((ViewPager) container).addView(itemView, 0);
        return itemView;
    }
    
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
      ((ViewPager) container).removeView((RelativeLayout) object);
    }
    
    public CharSequence getPageTitle (int position) {
        return label_ayam[position];
    }
}

}

您可以在主要活动中调用它:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    Button food = (Button)findViewById(R.id.food);
    food.setOnClickListener(new OnClickListener() {
        
        @SuppressWarnings("deprecation")
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent i = new Intent(getApplicationContext(), Food.class);
            startActivity(i);
        }
    });
}

这是 activity_main.xml :

<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">

<Button 
    android:id="@+id/food"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:onClick="food"
    android:text="Food"
    />
于 2013-09-26T03:55:02.480 回答