zoukankan      html  css  js  c++  java
  • 安卓学习第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) {
        
        }
        
    });
        }
    
        
    
    }

  • 相关阅读:
    数字图像处理(一)之灰度转换和卷积python实现
    ArcEngine+C# 森林资源仿真系统 核心代码
    Dijkstra和Floyd算法遍历图的核心
    python像操作文件一样操作内存的模块 StringIO
    python操作Redis方法速记
    python中时间处理标准库DateTime加强版库:pendulum
    utittest和pytest中mock的使用详细介绍
    《金字塔原理》 读书笔记
    python轻量级orm框架 peewee常用功能速查
    docker中安装的mysql无法远程连接问题解决
  • 原文地址:https://www.cnblogs.com/Yvettey-me/p/3932866.html
Copyright © 2011-2022 走看看