Android:ImageView应用之图片浏览器

时间:2022-09-19 08:36:16

ImageView控件实现的简单图片浏览器

一.纯显示图片:

引言:

读者在做这个东西的时候,需要自己把图片在源程序中导入。

Android:ImageView应用之图片浏览器

读者要注意:所有导入的图片之前,图片的命名只可以是小写英文和数字。

效果图

Android:ImageView应用之图片浏览器Android:ImageView应用之图片浏览器

 关键代码片段:

imageView.setOnClickListener(new OnClickListener()

{

public void onClick(View v)

{                 imageView.setImageResource(images[++currentImg%images.length]);

}

});

其中加了黄色背景的代码循环显示图片。

全部代码:

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView; public class MainActivity extends Activity { int[] images=new int[]{R.drawable.lrp1,
R.drawable.lrp2,
R.drawable.ls,
R.drawable.mr};
int currentImg=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//LinearLayout main= (LinearLayout) findViewById(R.id.root);
final ImageView imageView = (ImageView) findViewById(R.id.image);
imageView.setImageResource(images[0]);
imageView.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
// TODO Auto-generated method stub
imageView.setImageResource(images[++currentImg%images.length]);
}
}); } @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} }
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00f"
android:layout_marginTop="10dp"/>
</LinearLayout>

二.通过使用其它控件控制图片:

关键代码:

next.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
image1.setImageResource(images[++currentImg%images.length]);
} });
OnClickListener listener=new OnClickListener()
{
@SuppressWarnings("deprecation")
public void onClick(View v)
{
if(v == plus)
{
alpha += 20;
if(alpha >=255)
{
alpha=255;
}
}
else if(v==minus)
{
alpha -= 20;
if(alpha <= 0)
{
alpha = 0;
}
}
image1.setAlpha(alpha);
} };
plus.setOnClickListener(listener);
minus.setOnClickListener(listener);
}

简而言之:在监听中添加对ImageView属性的控制。

全部代码:

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.view.Menu;
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;
import android.widget.TabHost; public class MainActivity extends Activity {
private int[] images=new int[]{R.drawable.lrp1,
R.drawable.lrp2,
R.drawable.ls,
R.drawable.mr};
private int currentImg=0;
private int alpha=255;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//得到引用
final ImageView image1 = (ImageView) findViewById (R.id.image1);
final Button plus = (Button) findViewById(R.id.button1);
final Button minus = (Button) findViewById(R.id.button2);
final Button next = (Button) findViewById(R.id.button3);
//设置监听按钮
next.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
image1.setImageResource(images[++currentImg%images.length]);
} });
OnClickListener listener=new OnClickListener()
{
@SuppressWarnings("deprecation")
public void onClick(View v)
{
if(v == plus)
{
alpha += 20;
if(alpha >=255)
{
alpha=255;
}
}
else if(v==minus)
{
alpha -= 20;
if(alpha <= 0)
{
alpha = 0;
}
}
image1.setAlpha(alpha);
} };
plus.setOnClickListener(listener);
minus.setOnClickListener(listener);
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
} }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
> <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="增大透明度"
android:gravity="left" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="降低透明度"
android:gravity="left"/>
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:text="下一张" />
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
> <ImageView
android:id="@+id/image1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitCenter"
android:src="@drawable/lrp1" /> </LinearLayout>
</LinearLayout>

Android:ImageView应用之图片浏览器的更多相关文章

  1. Android ImageView显示本地图片

    Android ImageView 显示本地图片 布局文件 <?xml version="1.0" encoding="utf-8"?> <R ...

  2. Android ImageView&lpar;scaleType属性&rpar;图片按比例缩放

    <ImageView android:id="@+id/img" android:src="@drawable/logo" android:scaleTy ...

  3. Android imageview显示圆形图片

    需要ImageView显示圆形图片做法如下 public static Bitmap toRoundCorner(Bitmap bitmap, float ratio) { System.out.pr ...

  4. Android小案例——简单图片浏览器

    今天上午休息看Android书,里面有个变化图片的示例引起了我的兴趣. 示例需求: 有N张图片,循环显示图片的内容.如果需求让我写我会使用一个变量count来保存显示图片数据的索引,图片显示时做个判断 ...

  5. Android ImageView高度根据图片比例自适应

    设置adjustViewBounds // 是否保持宽高比 <ImageView android:id="@+id/iv_test" android:layout_width ...

  6. Android ImageView圆形头像

    转载自:http://m.oschina.net/blog/321024 Android ImageView圆形头像 图片完全解析 我们在做项目的时候会用到圆形的图片,比如用户头像,类似QQ.用户在用 ...

  7. 【转】Android ImageView圆形头像

    Android ImageView圆形头像 图片完全解析 我们在做项目的时候会用到圆形的图片,比如用户头像,类似QQ.用户在用QQ更换头像的时候,上传的图片都是矩形的,但显示的时候确是圆形的. 原理: ...

  8. 一步一步打造自己的Android图片浏览器(原创)

    今天我们试着来制作一个自己的Android图片浏览器. 图片浏览器应该具有什么功能呢?鉴于不同的人不同的理解,这里提出一个基本的需求: 搜索手机内的所有图片,展示于一个列表中: 列表中展示的是图片的缩 ...

  9. Android中轴旋转特效实现,制作别样的图片浏览器

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/10766017 Android API Demos中有很多非常Nice的例子,这些例 ...

随机推荐

  1. iOS 中constraint 不等于约束和低优先级约束使用的简单体会

    看了些文章发现,在使用constraint时,不等于约束往往是和低优先级约束成对使用的,这样才能实现他们的效果. 看看例子 下面是在3.5存屏幕下的效果 图1,竖屏,在满足>=50的前提下,可以 ...

  2. 表中查询重复的数据,如何通过sql语句查询?

    1.最直观的思路:要知道所有名字有重复人资料,首先必须知道哪个名字重复了:select name from emp group by name having count(*)>1所有名字重复人的 ...

  3. C&num; window 窗体 保持最前显示

    两句话搞定 [DllImport("user32.dll", CharSet = CharSet.Auto)]  private static extern int SetWind ...

  4. git的使用,eclipse操作(待更新)

    使用eclipse将项目上传到git私有服务器或github服务器: 1.配置个人信息: 2.配置eclipse私钥公钥: 上传公钥到仓库: 修改仓库中的公钥名称: 3.提交代码 一个仓库只能上传一个 ...

  5. Cow Contest POJ - 3660

    题意 有n(1<=n<=100)个学生参加编程比赛. 给出m条实力信息.(1<=M<=4500) 其中每一条的格式为 A B (1<=A<=N,1<=B&lt ...

  6. AX&lowbar;List

    List list = new List(Types::Class);  CustTable custTable;  while select custTable  {      list.addEn ...

  7. python 协程库gevent学习--gevent源码学习&lpar;二&rpar;

    在进行gevent源码学习一分析之后,我还对两个比较核心的问题抱有疑问: 1. gevent.Greenlet.join()以及他的list版本joinall()的原理和使用. 2. 关于在使用mon ...

  8. 自学Python1&period;7-python变量以及类型

    自学Python之路 自学Python1.7-python 变量以及类型 1 变量是什么 变量是容器 2 变量的作用 存储数据到内存 3 为什么要用变量 存储数据方便后面引用 4 变量定义的规范 变量 ...

  9. 在Asp&period;Net MVC中使用Repeater控件

    使用Repeater控件在视图中展示图表信息,Repeater控件的使用概述: <asp:Repeater ID="Repeater1" runat="server ...

  10. vue语法小练习

    实现功能:新增/删除 学生 <html> <head> <script src="https://cdn.staticfile.org/vue/2.2.2/vu ...