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;
}
}
<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;
}
}
相关文章推荐
- poj3304 判断是否存在一条直线经过n条线段
- 你为什么不分享
- C基础--猜数字游戏(图形界面由MFC完成)
- 信息学奥林匹克竞赛-Guide教程
- android_Intent对象初步(Activity传统的价值观念)
- Ogre源代码浅析——插件(Plugin)工作原理
- PullToRefresh开源控件和5.0新特性SwipeRefreshLayout的对比使用
- 表单(单选,复选,文本输入)
- Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: 数据库 'BHIoTV1.1' 的事务日志已满。若要查明无法重用日志中的空间的原
- hihoCoder1227 The Cats' Feeding Spots 二分
- sqlserver2008 死锁解决方法及性能优化方法
- Windows Azure 上传 VM
- 软件测试之性能测试
- 优先队列
- log4j简单使用
- Linux查看系统配置常用命令
- 【转载】ASP.Net中的IsPostBack
- 杭电1009
- 安全测试学习笔记一(Cookie&Session)(摘要)
- 并发编程之可变状态