android常用控件(二)
16lz
2021-01-24
ToggleButton(点击开关)
Switch(滑动开关)
SeekBar(进度条)
RatingBar(评分控件)
用这4个控件来做一个简单运用
效果图是这样的
xml代码
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="410dp" android:layout_height="500dp"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/iv_work_image" /> LinearLayout> <SeekBar android:id="@+id/sb_work_seek" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <ToggleButton android:layout_width="60dp" android:layout_height="60dp" android:id="@+id/tb_work_open1" android:onClick="open1" /> <Switch android:layout_width="60dp" android:layout_height="60dp" android:layout_gravity="center" android:id="@+id/tb_work_open2" android:onClick="open2" android:checked="true"/> <RatingBar android:id="@+id/rb_work_ratingBar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" /> LinearLayout>LinearLayout>
java代码
package com.example.a_02.a_06;import android.content.Intent;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.os.Bundle;import android.os.Environment;import android.support.annotation.Nullable;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.ImageView;import android.widget.RatingBar;import android.widget.SeekBar;import android.widget.Switch;import android.widget.TextView;import android.widget.Toast;import android.widget.ToggleButton;import java.io.File;import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;/** * Created by ming on 2017/6/11 0011. */public class work extends AppCompatActivity { private File[] files; private ImageView iv_work_image; private Bitmap bitmap; private int qq=1; private ToggleButton tb_work_open1; private Switch tb_work_open2; private SeekBar sb_work_seek; private RatingBar rb_work_ratingBar; private RatingBar rb; private Map count = new HashMap(); @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.work); iv_work_image = (ImageView) findViewById(R.id.iv_work_image); tb_work_open1 = (ToggleButton) findViewById(R.id.tb_work_open1); tb_work_open2 = (Switch) findViewById(R.id.tb_work_open2); sb_work_seek = (SeekBar) findViewById(R.id.sb_work_seek); rb_work_ratingBar = (RatingBar) findViewById(R.id.rb_work_ratingBar); //内存卡激活 if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ //获取内存卡路劲 String ss=Environment.getExternalStorageDirectory().getAbsolutePath();//获取内存卡绝对路径 File file=new File(ss+"/images");//获取文件夹路径 files=file.listFiles(); //获取文件夹下所有的文件// 使用bitmap位图 bitmap = BitmapFactory.decodeFile(files[qq].getAbsolutePath()); iv_work_image.setImageBitmap(bitmap); //SeekBar监听 sb_work_seek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { seekBar.setMax(255); iv_work_image.setImageAlpha(progress); //Toast.makeText(work.this, progress+"", Toast.LENGTH_SHORT).show(); } @Override public void onStartTrackingTouch(SeekBar seekBar) { //Toast.makeText(work.this, "触摸", Toast.LENGTH_SHORT).show(); } @Override public void onStopTrackingTouch(SeekBar seekBar) { //Toast.makeText(work.this, "放开", Toast.LENGTH_SHORT).show(); } }); //RatingBar监听 rb_work_ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() { @Override public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) { count.put(qq,rating); Toast.makeText(work.this, count.size()+"kkkkk", Toast.LENGTH_SHORT).show(); } }); } } public void open1(View view) { if (tb_work_open1.isChecked() == false) { qq=qq+1; if(qq>=files.length){ qq=1; } bitmap = BitmapFactory.decodeFile(files[qq].getAbsolutePath()); iv_work_image.setImageBitmap(bitmap); } else { qq=qq+1; if(qq>=files.length){ qq=1; }// Toast.makeText(work.this, qq+"777", Toast.LENGTH_SHORT).show(); bitmap = BitmapFactory.decodeFile(files[qq].getAbsolutePath()); iv_work_image.setImageBitmap(bitmap); } if(count.get(qq)==null){ String ss="3"; count.put(qq, Float.parseFloat(ss)); } rb_work_ratingBar.setRating(count.get(qq)); } public void open2(View view){ if(tb_work_open2.isChecked()==true){ bitmap = BitmapFactory.decodeFile(files[qq].getAbsolutePath()); iv_work_image.setImageBitmap(bitmap); //Toast.makeText(this,"打开" , Toast.LENGTH_SHORT).show(); }else{ bitmap=null; iv_work_image.setImageBitmap(bitmap); // Toast.makeText(this,"关闭" , Toast.LENGTH_SHORT).show(); } }}
左边开关换图,右边是关闭看图
星星则是评分
上面的进度条则可以调图片透明度
更多相关文章
- Android(安卓)databinding RecycleView item 子控件点击事件
- Android:ListView优化
- android按屏幕大小动态确定控件位置及大小
- UI 开源代码 FileBrowserView
- Android(安卓)初始化NavigationView的headerLayout中的TextView
- Android模仿文件夹管理实例
- Android(安卓)常用命令
- Android(安卓)带清除功能的输入框控件实例详解
- android 自定义控件之折线图自己写代码,不用jar包