1.xml代码的实现
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.zking.admin.android_07.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/b_main_before"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="<"
android:onClick="before"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
android:onClick="add"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
android:onClick="sub"/>
<Button
android:id="@+id/b_main_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=">"
android:onClick="next"/>
</LinearLayout>
<ImageView
android:id="@+id/iv_image_image"
android:layout_width="350dp"
android:layout_height="330dp"
android:background="#ff0000"
/>
<ImageView
android:id="@+id/iv_image_new"
android:layout_width="70dp"
android:layout_height="70dp"
android:background="#ff0000"
/>
<SeekBar
android:id="@+id/sb_main_seekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:progress="0"
android:max="255"
/>
<Switch
android:id="@+id/s_main_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ToggleButton
android:id="@+id/tb_main_toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
2.java代码实现
package com.zking.admin.android_07;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.SeekBar;
import android.widget.Switch;
import android.widget.Toast;
import android.widget.ToggleButton;
import java.io.File;
public class MainActivity extends AppCompatActivity {
private ImageView iv_image_image;
private int images[]={R.drawable.s1,
R.drawable.s2,
R.drawable.s3,
R.drawable.s4,
R.drawable.s5
};
int currentIndex=0;
int currentAlpha=255;
private File[] files;
private Bitmap bm;
private ImageView iv_image_new;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv_image_image = (ImageView) findViewById(R.id.iv_image_image);
iv_image_new = (ImageView) findViewById(R.id.iv_image_new);
final SeekBar seek= (SeekBar) findViewById(R.id.sb_main_seekbar);
seek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
iv_image_image.setImageAlpha(progress);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
Switch sw= (Switch) findViewById(R.id.s_main_switch);
sw.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
Toast.makeText(MainActivity.this,"开",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this,"关",Toast.LENGTH_SHORT).show();
}
}
});
ToggleButton toggle= (ToggleButton) findViewById(R.id.tb_main_toggle);
toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
Toast.makeText(MainActivity.this,"开",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this,"关",Toast.LENGTH_SHORT).show();
}
}
});
//判断 手机是否有内存卡 内存卡是否可用
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
//获取手机内存卡路径
String sdCardPath=Environment.getExternalStorageDirectory().getAbsolutePath();
//获取手机内存卡目录中的图片
File file=new File(sdCardPath+"/images");
files = file.listFiles();
}
// iv_image_image.setImageResource(images[currentIndex]);
bm = BitmapFactory.decodeFile(files[0].getAbsolutePath());
iv_image_image.setImageBitmap(bm);
iv_image_image.setImageAlpha(currentAlpha);
//给图片控件设置触摸事件
iv_image_image.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
//获取触摸的坐标
float x=motionEvent.getX();
float y=motionEvent.getY();
iv_image_image.setDrawingCacheEnabled(true);
bm=iv_image_image.getDrawingCache();
//截取
//进行边缘判断
//左下角
if(y+70>bm.getHeight()){
y=y-70;
}
//右上角
if(x+70>bm.getWidth()){
x=x-70;
}
Bitmap bmNew=Bitmap.createBitmap(bm,(int)(x),(int)(y),70,70);
iv_image_new.setImageBitmap(bmNew);
iv_image_image.setDrawingCacheEnabled(false);
return true;
}
});
}
public void before(View view){
currentIndex--;
if(currentIndex<0){
currentIndex=0;
Toast.makeText(MainActivity.this, "兄弟,没得选了", Toast.LENGTH_SHORT).show();
}
//iv_image_image.setImageResource(images[currentIndex]);
bm= BitmapFactory.decodeFile(files[currentIndex].getAbsolutePath());
iv_image_image.setImageBitmap(bm);
}
public void next(View view){
currentIndex++;
if(currentIndex>=images.length){
currentIndex=images.length-1;
Toast.makeText(MainActivity.this, "兄弟,没得选了", Toast.LENGTH_SHORT).show();
}
//iv_image_image.setImageResource(images[currentIndex]);
bm= BitmapFactory.decodeFile(files[currentIndex].getAbsolutePath());
iv_image_image.setImageBitmap(bm);
}
public void add(View view){
currentAlpha-=20;
if(currentAlpha<=0){
currentAlpha=0;
Toast.makeText(MainActivity.this, "看不见了", Toast.LENGTH_SHORT).show();
}
iv_image_image.setImageAlpha(currentAlpha);
}
public void sub(View view){
currentAlpha+=20;
if(currentAlpha>=255){
currentAlpha=255;
Toast.makeText(MainActivity.this, "都给你了", Toast.LENGTH_SHORT).show();
}
iv_image_image.setImageAlpha(currentAlpha);
}
public void getxin(View view){
}
}