十一、Gallery、seekbar简单应用

Gallery

一、绘制页面

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <Gallery
        android:id="@+id/gallery_gallery"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></Gallery>

    <ImageSwitcher
        android:id="@+id/gallery_imageswitch"
        app:layout_constraintTop_toBottomOf="@+id/gallery_gallery"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></ImageSwitcher>
</android.support.constraint.ConstraintLayout>

二、写一个adapter适配器

iamgadapter.java

package com.example.administrator.listview;

import android.content.Context;
import android.os.IInterface;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

/**
 * Created by Administrator on 2018/3/17 0017.
 */

public class iamgeadapter extends BaseAdapter{
    private int[]res;
    private Context context;
    public iamgeadapter(int []res,Context context){
        this.res=res;
        this.context=context;
    }
    @Override
    //返回已定义的数据源的总数量
    public int getCount() {
        //循环显示效果
        return Integer.MAX_VALUE;
    }
    //告诉适配器取得目前容器的数据id和对象
    @Override
    public Object getItem(int i) {
        return res[i];
    }

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

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ImageView image=new ImageView(context);
        //循环显示效果
        image.setBackgroundResource(res[i%res.length]);
        image.setLayoutParams(new Gallery.LayoutParams(400,300));
        image.setScaleType(ImageView.ScaleType.FIT_XY);
        return image;
    }
}


三、进行一些数据绑定工作

package com.example.administrator.listview;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.ViewSwitcher;

/**
 * Created by Administrator on 2018/3/17 0017.
 */

public class gallery extends AppCompatActivity implements AdapterView.OnItemSelectedListener,ViewSwitcher.ViewFactory{
    private Gallery gallery;
    //准备数组源
    private int[]res={R.drawable.draw1,R.drawable.draw2,R.drawable.draw3,R.drawable.draw4,R.drawable.draw5,R.drawable.draw6,R.drawable.draw7,R.drawable.draw8,R.drawable.draw9};
    private iamgeadapter imagadapter;
    private ImageSwitcher imageSwitcher;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gallery);
        gallery=findViewById(R.id.gallery_gallery);
        imageSwitcher=findViewById(R.id.gallery_imageswitch);
        //让gallery加载适配器
        imagadapter=new iamgeadapter(res,this);
        gallery.setAdapter(imagadapter);
        gallery.setOnItemSelectedListener(this);
        imageSwitcher.setFactory(this);
        imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_in));
        imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_out));
    }

    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
//        image.setBackgroundResource(res[i%res.length]);
        imageSwitcher.setBackgroundResource(res[i%res.length]);
    }

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

    }

    @Override
    public View makeView() {
        ImageView imageView=new ImageView(this);
        //大图预览效果
        imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
        return imageView;
    }
}

四、显示效果



seekbar

一、绘制页面

扫描二维码关注公众号,回复: 11582585 查看本文章

seekbar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <SeekBar
        android:id="@+id/seekbar_bar"
        style="@android:style/Widget.SeekBar"
        android:thumb="@drawable/my_thumb"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="100"
        android:progress="50"/>
    <TextView
        android:id="@+id/seekbar_textview1"
        app:layout_constraintTop_toBottomOf="@+id/seekbar_bar"
        android:text="text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/seekbar_textview2"
        app:layout_constraintTop_toBottomOf="@+id/seekbar_textview1"
        android:text="text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</android.support.constraint.ConstraintLayout>

二、自定义一个滑块的样式

my_thum.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/thumb1" android:state_pressed="true"></item>
    <item android:drawable="@drawable/thumb1" android:state_focused="true"></item>
    <item android:drawable="@drawable/thumb1" android:state_selected="true"></item>
    <item android:drawable="@drawable/thumb2"></item>
</selector>

三、seekbar.java

package com.example.administrator.listview;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.SeekBar;
import android.widget.TextView;

/**
 * Created by Administrator on 2018/3/17 0017.
 */

public class seekbar extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener{
    private SeekBar seekBar;
    private TextView tv1;
    private TextView tv2;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.seekbar);
        seekBar=findViewById(R.id.seekbar_bar);
        tv1=findViewById(R.id.seekbar_textview1);
        tv2=findViewById(R.id.seekbar_textview2);
        seekBar.setOnSeekBarChangeListener(this);
    }
    //数值改变
    @Override
    public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
        tv1.setText("正在拖动:");
        tv2.setText("当前数值:"+i);
    }
    //开始拖动
    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        tv1.setText("开始拖动:");
    }
    //停止拖动
    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        tv1.setText("停止拖动:");

    }
}




猜你喜欢

转载自blog.csdn.net/qq_38234785/article/details/79592597