首页 > 代码库 > 安卓学习第17课——Gallery
安卓学习第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) { } }); } }
安卓学习第17课——Gallery
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。