安卓学习第17课——Gallery

虽然Gallery已经过时了,但是既然书上讲了,我还要学习一下。。产生的效果很好。。。

<LinearLayout 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"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="320dp"
        android:layout_height="320dp"/>
    <Gallery
        android:id="@+id/gallery"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="25dp"
        android:unselectedAlpha="0.6"
        android:spacing="2pt" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="Gallery">
        <attr name="android:galleryItemBackground" />
    </declare-styleable>
</resources>
package com.example.gallery;
import android.app.Activity;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

@SuppressWarnings("deprecation")
public class MainActivity extends Activity {

    int[] imageIds=new int[]{
        R.drawable.shuangzi,R.drawable.shuangyu,R.drawable.chunv,
        R.drawable.tiancheng,R.drawable.tianxie,R.drawable.sheshou,R.drawable.juxie,
        R.drawable.shuiping,R.drawable.shizi,R.drawable.baiyang,R.drawable.jinniu,R.drawable.mojie
    };
    Gallery gallery;
    ImageView imageView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
gallery=(Gallery) findViewById(R.id.gallery);
imageView=(ImageView) findViewById(R.id.imageView);
BaseAdapter baseAdapter=new BaseAdapter(){

    @Override
    public int getCount() {
        return imageIds.length;
    }

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView=new ImageView(MainActivity.this);
    imageView.setImageResource(imageIds[position]);
    imageView.setScaleType(ImageView.ScaleType.FIT_XY);
    imageView.setLayoutParams(new Gallery.LayoutParams(75,100));
    TypedArray typedArray = obtainStyledAttributes(
            R.styleable.Gallery);
    imageView.setBackgroundResource(typedArray.getResourceId(
            R.styleable.Gallery_android_galleryItemBackground, 0));
    return imageView;
    }

};
gallery.setAdapter(baseAdapter);
gallery.setOnItemSelectedListener(new OnItemSelectedListener(){

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position,
            long id) {
        imageView.setImageResource(imageIds[position]);

    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }

});
    }

}

时间: 2024-08-02 05:00:40

安卓学习第17课——Gallery的相关文章

安卓学习第13课——BaseAdapter

BaseAdapter创建这么一个对象,需要些四个方法. int getCount(); Object getItem(int position); long getItemId(int position);View getView(int position, View convertView, ViewGroup parent);(1)列表中的项数(2)返回值的列表内容(3)获得postion处的列表项的ID(4)该列表项里的组件 package com.example.baseadapter

安卓学习第12课——SimpleAdapter

SimpleAdapter一点也不简单,他的参数就带了5个,天哪,晕了.. 今天学的这个适配器, public SimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) 看这个大概明白,参数分别是第一个:表示访问整个android应用程序接口,基本上所有的组件都需要,一般都写this(改天研究一下),第二个应该是这个List对象

安卓学习第9课——计时器chronometer

今天学习了钟表及计时器.. 我觉得AnalogClock和DigitalClock直接使用就可以.唯一需要知道的就是AnalogClock是可以修改表盘和分针时针的. 方法是android:dail及android:hand_minute和hand_hour. 下面介绍的是计时器的用法. 首先xml中只要放入一个chronometer和一个按钮即可.为的是是点击启动按钮,开始计时,20s停止. package com.example.chronometer; import android.app

安卓学习第11课——AutoCompleteTextView

...在百度上搜了这么一段.理解了ArrayAdapter的三个参数的用途 1. 这个小例子是要显示一个数组,我们就用ArrayAdapter,数组适配器,数据的数据类型<>是String类型的,数据的数据类型还可以是其他的包括对象类型的 2. ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(ArrayListDemo.this, android.R.layout.simple_list_item

安卓学习第10课——listview

1.普通listview <LinearLayout 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" androi

安卓学习第8课——开关ToggleButton、Switch

今天学的是对开关的监听,两种开关ToggleButton和switch <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="ma

安卓学习第三课——常见布局

1.相对布局 简单的说,就是通过描述每个组件所在的位置,使用的layout_below等,就是控制组件与组件之间的位置关系. 2.绝对布局 就是通过描述他的x,y坐标来确定位置 3.线性布局 有两种是水平和竖直对其方式,一般情况下整体会使用线性布局,来排列众多的组件 3.帧布局 我感觉就是一层一层的,默认的情况下,多个组件是在同一个位置,所以你需要去修改位置.同时可以选择是否显示. 这可以用来描述视频播放器暂停键的控制方法. 代码如下. <?xml version="1.0" e

安卓学习第7课——checkbutton

1.布局 <TableLayout 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" android:orienta

安卓学习第6课——button

今天学会了按钮的背景图片是可以改变的... 背景可以不是图片,而是一个xml文件. <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="带文字的图片按钮" android:background="@drawable/button_selector" android:textSize=&