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) {
        
        }
        
    });
        }
    
        
    
    }

  • 相关阅读:
    《中断学习(一) —— Linux中断流程以及处理机制》
    《C语言知识点 —— 字符串指针和字符串数组的区别》
    《驱动实例 —— 触发外部中断后通过异步通知机制发送信号给用户态》
    《驱动学习 —— 杂项设备》
    玩转----adb adb monkey命令及介绍
    玩转----黑盒技术设计测试用例的方法主要有
    玩转----LoadRunner具体流程
    玩转----软件立项阶段
    玩转----Linux之ant安装部署
    玩转----Ubuntu 16.04安装JDK并配置环境变量
  • 原文地址:https://www.cnblogs.com/Yvettey-me/p/3932866.html
Copyright © 2011-2022 走看看