您的位置:首页 > 其它

ImageView图片轮播,放大缩小,改变图片透明度

2015-09-21 08:58 375 查看
Layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="360dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center">
<Button
android:id="@+id/last"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="上一张"/>
<Button
android:id="@+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下一张"/>
<Button
android:id="@+id/smaller"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="缩小"/>
<Button
android:id="@+id/bigger"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="放大"/>
<Button
android:id="@+id/qinger"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="减透"/>
</LinearLayout>"
</LinearLayout>

</RelativeLayout>


JAVA

package com.example.imageview;

import android.os.Bundle;
import android.view.View;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {

ImageView iv;
Button btLast,btNext,btSmall,btBig,btQing;
Bitmap bmp;
int[] img=new int[]{R.drawable.img0,R.drawable.img1,R.drawable.img2,R.drawable.img3,R.drawable.img4};
int i=0,alpha=2255;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
bmp=BitmapFactory.decodeResource(getResources(), img[i]);
iv.setImageBitmap(bmp);
iv.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
if(alpha>=20){
alpha-=20;
iv.setAlpha(alpha);
}

}
});
btLast.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
if(i==0)
i=5;
i--;
iv.setImageResource(img[i]);
bmp=BitmapFactory.decodeResource(getResources(), img[i]);
alpha=255;

}
});
btNext.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
if(i==4)
i=-1;
i++;
iv.setImageResource(img[i]);
bmp=BitmapFactory.decodeResource(getResources(), img[i]);
alpha=255;

}
});
btSmall.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
bmp=scaleToFit(bmp,0.8f);
iv.setImageBitmap(bmp);
}
});
btBig.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
bmp=scaleToFit(bmp,1.2f);
iv.setImageBitmap(bmp);
}
});
btQing.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
if(alpha<=255){
alpha+=20;
iv.setAlpha(alpha);
}
}
});
}

private void init(){
btLast=(Button)this.findViewById(R.id.last);
btNext=(Button)this.findViewById(R.id.next);
btSmall=(Button)this.findViewById(R.id.smaller);
btBig=(Button)this.findViewById(R.id.bigger);
btQing=(Button)this.findViewById(R.id.qinger);
iv=(ImageView)this.findViewById(R.id.image);
}

protected Bitmap scaleToFit(Bitmap bmp2,float f){
int width=bmp2.getWidth();
int height=bmp2.getHeight();
Matrix matrix=new Matrix();
matrix.postScale(f, f);
Bitmap bmResult=Bitmap.createBitmap(bmp2, 0, 0, width, height, matrix, true);
return bmResult;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: