使用HorizontalScrollView和ImageView编写简单的图片展示程序
2015-07-10 18:17
429 查看
程序运行结果截图:
运行程序截图
MainActivity.java代码如下:
package com.example.helloworld;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class MainActivity extends Activity
{
private ImageView imgv;
LinearLayout h1;
int [] imgs=
{
R.drawable.img1,
R.drawable.img2,
R.drawable.img3,
R.drawable.img4,
R.drawable.img5,
R.drawable.img6,
R.drawable.img7,
R.drawable.img8
};
ImageView vs[];
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
h1=(LinearLayout)findViewById(R.id.h1);
imgv=(ImageView)findViewById(R.id.imgv);
imgv.setImageResource(R.drawable.img1);
vs=new ImageView[]
{
new ImageView(this),
new ImageView(this),
new ImageView(this),
new ImageView(this),
new ImageView(this),
new ImageView(this),
new ImageView(this),
new ImageView(this)
};
for(int i=0;i<
vs.length;i++)
{
vs[i].setImageResource(imgs[i]);
vs[i].setLayoutParams(new LayoutParams(120,120));
vs[i].setOnClickListener(new hkf());
h1.addView(vs[i]);
}
}
class hkf implements OnClickListener
{
public void onClick(View v)
{
int i;
for(i=0;i<
vs.length;i++)
{
if(v==vs[i])break;
}
imgv.setImageResource(imgs[i]);
}
}
}
activity_main.xml代码如下:
< 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="com.example.hkf4.MainActivity" >
< ImageView
android:id="@+id/imgv"
android:layout_width="400dp"
android:layout_height="400dp" />
< HorizontalScrollView
android:layout_width="fill_parent"
android:orientation="horizontal"
android:layout_height="wrap_content">
< LinearLayout
android:id="@+id/h1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
</ LinearLayout>
</ HorizontalScrollView>
</ RelativeLayout>
相关文章推荐
- android ImageView 的几点经验总结
- Android控件系列之ImageView使用方法
- Android中通过反射实现圆角ImageView代码实例
- ImageView的属性android:scaleType的作用分析
- Android优秀开源库收集
- android 实现圆形imageView,Circle imageView.
- imageview属性
- Android实现图片宽度100%ImageView宽度且高度按比例自动伸缩
- ImageView.ScaleType设置图解
- 关于Android 开源控件 photoview 的单击用法
- 仿QQ控件图片下拉放大效果
- Android的ImageView控件ScaleType属性详解
- 下拉放大tableView头视图
- Android自定义圆形图片
- 发散按钮浅谈
- java内存优化实例----在非UI线程中处理图片
- android学习笔记---图片拉伸和裁剪
- 1Android调用系统图库2读写文件
- image 在页面间的传递
- 自定义ImageView实现单点缩放回弹、拖拽、多点缩放功能