疯狂Android之图片浏览器
来源:互联网 发布:ipad可以开淘宝店吗 编辑:程序博客网 时间:2024/05/01 17:59
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
int[] images = new int[] { R.drawable.icon_two, R.drawable.fdsfdsafdsaf,
R.drawable.icon_odfde, R.drawable.icon_ofdfd };
int count = 2;
private int alpha = 255;
Button but_1, but_2, but_3;
ImageView imageView1, imageView2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
but_3 = (Button) findViewById(R.id.but_3);
but_1 = (Button) findViewById(R.id.but_1);
but_2 = (Button) findViewById(R.id.but_2);
imageView1 = (ImageView) findViewById(R.id.imageView1);
imageView2 = (ImageView) findViewById(R.id.imageView2);
but_3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
imageView1.setImageResource(images[++count % images.length]);
}
});
View.OnClickListener listtener = new View.OnClickListener() {
@Override
public void onClick(View v) {
if (v == but_1) {
alpha += 20;
}
if (v == but_2) {
alpha -= 20;
}
if (alpha >= 255) {
alpha += 255;
}
if (alpha <= 0) {
alpha += 0;
}
imageView1.setAlpha(alpha);
}
};
but_1.setOnClickListener(listtener);
but_2.setOnClickListener(listtener);
imageView1.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) imageView1
.getDrawable();
Bitmap bitmap = bitmapDrawable.getBitmap();
double scale = 1.0 * bitmap.getHeight() / bitmap.getWidth();
int x = (int) (event.getX() * scale);
int y = (int) (event.getY() * scale);
if (x + 120 > bitmap.getWidth()) {
x = bitmap.getWidth() - 120;
}
if (y + 120 > bitmap.getHeight()) {
y = bitmap.getHeight() - 120;
}
imageView2.setImageBitmap(Bitmap.createBitmap(bitmap, x, y,
120, 120));
return false;
}
});
}
}
xml文件activity_main
<LinearLayout 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:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/but_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+" />
<Button
android:id="@+id/but_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-" />
<Button
android:id="@+id/but_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下一張" />
</LinearLayout>
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="280dp"
android:scaleType="fitCenter"
android:src="@drawable/icon_two" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="120dp"
android:layout_height="120dp"
android:background="#00f" />
</LinearLayout>
- 疯狂Android之图片浏览器
- Android 简单图片浏览器(疯狂Android讲义第三版)
- Android开发之图片浏览器
- Android开发之图片浏览器
- Android学习之打造美女图片浏览器
- Android学习之优化美女图片浏览器
- Android之网络下载与图片解码-----网络图片浏览器
- java之图片浏览器
- swift之图片浏览器
- qt之图片浏览器
- Android图片浏览器
- android 图片浏览器 demo
- android高仿微信图片浏览器
- 【Android】简单图片浏览器
- Android 简单图片浏览器
- Android--网络图片浏览器
- 【Android 应用开发】AndroidUI设计 之 图片浏览器
- Android界面编程之简单的图片浏览器
- MySQL:数据备份与还原
- MySQL:MySQL日志
- mac os x使用Git简易入门教程
- MySQL:性能优化
- java 中文 乱码 读写
- 疯狂Android之图片浏览器
- MySQL:MySQL Replication
- 用eclipse浏览linux内核
- 解决Eclipse打开后闪退 Eclipse在异常关闭后,启动闪退的解决办法
- Spring4 MVC ajax 406问题
- editplus文本编辑在编程中的使用
- MySQL:MySQL+Cluster
- 嵌入式管脚定义
- Scrum 敏捷 项目管理工具