本文介绍: styleable.xml&lt;?xml version=”1.0″ encoding=”utf-8″?&gt;&lt;resources&gt; &lt;declare-styleable name=”Gallery“&gt; &lt;attr name= “android:galleryItemBackground” /> </declarestyleable></resources><?xml version.
styleable.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <declare-styleable name="Gallery">
        <attr name= "android:galleryItemBackground" />
    </declare-styleable>

</resources>

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center_horizontal"
    android:orientation="vertical">
    >

    <ImageSwitcher
        android:id="@+id/imageswitcher"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:paddingTop="30px"></ImageSwitcher>

    <Gallery
        android:id="@+id/gallery"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:spacing="5px"
        android:unselectedAlpha="0.6"></Gallery>


</LinearLayout>

package com.texxt;


import android.app.Activity;

import android.content.res.TypedArray;
import android.os.Bundle;


import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.Gallery;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RatingBar;
import android.widget.SeekBar;
import android.widget.SimpleAdapter;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewSwitcher;


import androidx.annotation.NonNull;

import com.example.textmethod.R;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

public class MainActivity extends Activity {

    int[] img = new int[]{R.drawable.img1, R.drawable.img2, R.drawable.img3, R.drawable.img1, R.drawable.img2, R.drawable.img3, R.drawable.img1, R.drawable.img2, R.drawable.img3};

    public ImageSwitcher imageSwitcher;
    public Gallery gallery;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        gallery = findViewById(R.id.gallery);
        imageSwitcher = findViewById(R.id.imageswitcher);

        imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
        imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
        imageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
            @Override
            public View makeView() {
                ImageView imageView=new ImageView(MainActivity.this);
                imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
                imageView.setLayoutParams(new ImageSwitcher.LayoutParams(500, 500));
                return imageView;
            }
        });
        imageSwitcher.setImageResource(img[0]);
        BaseAdapter adapter = new BaseAdapter() {
            @Override
            public int getCount() {
                return img.length;
            }

            @Override
            public Object getItem(int i) {
                return img[i];
            }

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

            @Override
            public View getView(int i, View view, ViewGroup viewGroup) {
                ImageView imageView;
                if (view == null) {
                    imageView = new ImageView(MainActivity.this);

                    imageView.setPadding(5, 0, 5, 0);
                    imageView.setLayoutParams(new Gallery.LayoutParams(250, 250));
                    imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
                    TypedArray typedArray = obtainStyledAttributes(R.styleable.Gallery);
                    imageView.setBackgroundResource(typedArray.getResourceId(R.styleable.Gallery_android_galleryItemBackground, 0));
                } else {
                    imageView = (ImageView) view;
                }
                imageView.setImageResource(img[i]);
                return imageView;
            }
        };
        gallery.setAdapter(adapter);

        gallery.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                imageSwitcher.setImageResource(img[i]);
            }
        });

    }
}

原文地址:https://blog.csdn.net/qq_32659773/article/details/124281587

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。

如若转载,请注明出处:http://www.7code.cn/show_10107.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注