Android中ImageView用法实例分析
作者:马到成功 发布时间:2023-04-03 19:42:28
标签:Android,ImageView
本文实例分析了Android中ImageView用法。分享给大家供大家参考,具体如下:
猜牌游戏大家可能以前都玩过,这里我们用这个小游戏来说明ImageView的用法。
首先,在res/drawable中引入三张牌:分别是梅花7,梅花8,梅花9
然后在res/layout/main.xml中配置一个TextView,三个ImageView以及一个Button
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<ImageView
android:id="@+id/iv07"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<ImageView
android:id="@+id/iv08"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<ImageView
android:id="@+id/iv09"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
程序如下所示:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class A04Activity extends Activity {
private ImageView iv07,iv08,iv09;
private TextView tv;
private Button b;
private int[] s={
R.drawable.puke07,
R.drawable.puke08,
R.drawable.puke09
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
iv07=(ImageView)findViewById(R.id.iv07);
iv08=(ImageView)findViewById(R.id.iv08);
iv09=(ImageView)findViewById(R.id.iv09);
tv=(TextView)findViewById(R.id.tv);
b=(Button)findViewById(R.id.button);
randon();
//下面以ImageView的OnClickListener()方法对选择的不同牌做不同的反应
iv07.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
iv07.setImageDrawable(getResources().getDrawable(s[0]));
iv08.setImageDrawable(getResources().getDrawable(s[1]));
iv09.setImageDrawable(getResources().getDrawable(s[2]));
iv08.setAlpha(100); //对没有选择的牌做灰暗处理
iv09.setAlpha(100);
if(s[0]==R.drawable.puke08){ //如果选择的牌是梅花8的话就猜对了
tv.setText("恭喜你,猜对了!!!");
}
else{
tv.setText("亲,猜错了,要不要再来一次?");
}
}
});
iv08.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
iv07.setImageDrawable(getResources().getDrawable(s[0]));
iv08.setImageDrawable(getResources().getDrawable(s[1]));
iv09.setImageDrawable(getResources().getDrawable(s[2]));
iv07.setAlpha(100);
iv09.setAlpha(100);
if(s[1]==R.drawable.puke08){
tv.setText("恭喜你,猜对了!!!");
}
else{
tv.setText("亲,猜错了,要不要再来一次?");
}
}
});
iv09.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
iv07.setImageDrawable(getResources().getDrawable(s[0]));
iv08.setImageDrawable(getResources().getDrawable(s[1]));
iv09.setImageDrawable(getResources().getDrawable(s[2]));
iv07.setAlpha(100);
iv08.setAlpha(100);
if(s[2]==R.drawable.puke09){
tv.setText("恭喜你,猜对了!!!");
}
else{
tv.setText("亲,猜错了,要不要再来一次?");
}
}
});
b.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
tv.setText("猜猜梅花8在哪里");
iv07.setImageDrawable(getResources().getDrawable(R.drawable.puke00));
//刚开始的时候显示扑克牌的背面
iv08.setImageDrawable(getResources().getDrawable(R.drawable.puke00));
iv09.setImageDrawable(getResources().getDrawable(R.drawable.puke00));
iv07.setAlpha(255);//
iv08.setAlpha(255);
iv09.setAlpha(255);
randon();
}
});
}
//randon方法是进行随机地洗牌
public void randon(){
for(int i=0;i<s.length;i++){
int tmp=s[i];
int a=(int)(Math.random()*2);
s[i]=s[a];
s[a]=tmp;
}
}
}
希望本文所述对大家Android程序设计有所帮助。


猜你喜欢
- 什么是banner组件?在许多Android应用上,比如爱奇艺客户端、百度美拍、应用宝等上面,都有一个可以手动滑动的小广告条,这就是bann
- 相关函数:longjmp, siglongjmp, setjmp表头文件:#include <setjmp.h>函数定义:int
- 本文实例分析了c#中Empty()和DefalutIfEmpty()用法。分享给大家供大家参考。具体分析如下:在项目中,当我们想获取IEnu
- 随着使用Spring进行开发的个人和企业越来越多,Spring从一个单一简介的框架变成了一个大而全的开源软件,最直观的变化就是Spring需
- Android图片的处理工具类BitmapUtils,供大家参考,具体内容如下项目中经常会用到图片,所以在这先简单的总结一下。闲言少叙,上代
- 定义:将一个请求封装成一个对象,从而让你使用不同的请求把客户端参数化,对请求排队或者记录请求日志,可以提供命令的撤销和恢复功能。类型:行为类
- Step 1.依赖bannerGradledependencies{ compile 'com.youth.banner
- 在Android开发中,我们经常遇到Android读取图片大小超过屏幕显示的图(一般只要显示一定规格的预览图即可),在图片特别多或者图片显示
- 一、问题定义:问下有一个数组,这些数组中的值都有自己的权重,怎样设计才能高效的优先取出权重高的数??例如:权重: 8 2&nbs
- 前言:Java数据结构与算法专题会不定时更新,欢迎各位读者监督。本文从最简单的一个排序算法——桶排序开始,分析桶排序的实现思路,代码实现,性
- 一、概述有不少人问我intellij idea的mybatis-plus自动生成插件使用出现各种各样的问题,所以我在这边博客的基础上添加一些
- 本文实例讲述了Android DatePicker和DatePickerDialog基本用法。分享给大家供大家参考,具体如下:DatePic
- 1)打开idea,开始创建SpringBoot项目2)选择 Spring Initializr ,选择合适的jdk版本,点击Next在操作到
- C++对string进行大小写转换操作方法方法一:使用C语言之前的方法,使用函数,进行转换#include <iostream>
- 1.简介 现在android应用中不可避免的要使用图片,有些图片是可以变化的,需要每次启动时从网络拉取,这种场景在有广告位的应用以及纯图片应
- 前言开篇一个例子,我看看都有谁会?如果不会的,或者不知道原理的,还是老老实实看完这篇文章吧。@Slf4j(topic = "c.V
- 本文分享了c#操作Excel的相关代码,还是比较全面的,其实无外乎存取,增删改查等操作,参考下。具体代码://引用Microsoft.Off
- 前言老规矩,还是从最简单粗暴的开始。那么多简单算简单?多粗暴算粗暴?我告诉你可以不写一句代码,你信吗?直接把一个文件往IIS服务器上一扔,就
- 本文实例讲述了java中struts2实现文件上传下载功能实现方法。分享给大家供大家参考。具体分析如下:1.文件上传首先是jsp页面的代码在
- 一、代理模式代理模式的英文叫做Proxy或Surrogate,中文都可译为”代理“,所谓代理,就是一个人或者一个机构代表另一个人或者另一个机