当前位置: 代码迷 >> 综合 >> 十一、Gallery、seekbar简单应用
  详细解决方案

十一、Gallery、seekbar简单应用

热度:73   发布时间:2023-12-13 23:03:41.0

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"><Galleryandroid:id="@+id/gallery_gallery"android:layout_width="match_parent"android:layout_height="wrap_content"></Gallery><ImageSwitcherandroid: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和对象@Overridepublic Object getItem(int i) {return res[i];}@Overridepublic long getItemId(int i) {return i;}@Overridepublic 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;@Overrideprotected 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));}@Overridepublic void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
//        image.setBackgroundResource(res[i%res.length]);imageSwitcher.setBackgroundResource(res[i%res.length]);}@Overridepublic void onNothingSelected(AdapterView<?> adapterView) {}@Overridepublic View makeView() {ImageView imageView=new ImageView(this);//大图预览效果imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);return imageView;}
}

四、显示效果



seekbar

一、绘制页面

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"><SeekBarandroid: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"/><TextViewandroid:id="@+id/seekbar_textview1"app:layout_constraintTop_toBottomOf="@+id/seekbar_bar"android:text="text"android:layout_width="match_parent"android:layout_height="wrap_content" /><TextViewandroid: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;@Overrideprotected 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);}//数值改变@Overridepublic void onProgressChanged(SeekBar seekBar, int i, boolean b) {tv1.setText("正在拖动:");tv2.setText("当前数值:"+i);}//开始拖动@Overridepublic void onStartTrackingTouch(SeekBar seekBar) {tv1.setText("开始拖动:");}//停止拖动@Overridepublic void onStopTrackingTouch(SeekBar seekBar) {tv1.setText("停止拖动:");}
}




  相关解决方案