首页 > 分享 > Android UI之ImageView图片视图

Android UI之ImageView图片视图

小痕 于 2011-12-15 16:34:58 发布

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

ImageView是一个显示图片的组件,用一个例子介绍该组件的简单运用:

在样式文件中:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

<ImageView

android:id="@+id/img1"

android:layout_width="fill_parent"

android:layout_height="300dp"

android:background="#cccccc"

android:src="@drawable/pig" />

<ImageView

android:id="@+id/img2"

android:layout_width="100dp"

android:layout_height="100dp"

android:background="#cccccc"

android:scaleType="fitStart"

android:layout_marginTop="20dp"

/>

</LinearLayout>

该样式文件中有两个ImageView组件,第一个用来显示我们想要展示的图片,第二ImageView用来显示当点击图片上某一位置时在该组件上显示部分图片,代码实现该功能:


package cn.class3g.activity;

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.OnTouchListener;

import android.widget.ImageView;

public class ImageViewDemo extends Activity implements OnTouchListener {

ImageView imageView1, imageView2;

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

this.setContentView(R.layout.imageview_layout);

findViews();

}

private void findViews() {

imageView1 = (ImageView) findViewById(R.id.img1);

imageView2 = (ImageView) findViewById(R.id.img2);

//为imageView添加触摸监听事件

imageView1.setOnTouchListener(this);

}

public boolean onTouch(View v, MotionEvent event) {

float scale = 412 / 320;

//获取需要显示的图片的开始点

int x = (int) (event.getX() * scale);

int y = (int) (event.getY() * scale);

//需要考虑边界问题

int width = (int) (100 * scale);

int height = (int) (100 * scale);

//获取图片显示框中的位图

BitmapDrawable bitmapDrawable = (BitmapDrawable) imageView1.getDrawable();

//显示图片指定的区域

imageView2.setImageBitmap(Bitmap.createBitmap(bitmapDrawable.getBitmap(),

x,y, width, height));

return false;

}

}

在模拟器上的效果为

相关知识

基于TensorFlow2.3.0的花卉识别Android APP设计
实现类似android:clipChildren=“false”的效果,在使用android:clipChildren多次尝试无效时使用
基于TensorFlow Lite实现的Android花卉识别应用
Android多个Activity切换时其生命周期中的方法执行顺序
花朵纹样图片
复古的玫瑰花图片
创意花卉图案,由美丽的鸢尾花在柔和的紫色背景。自然的概念。最小的风格。前视图。平躺图片
创意的夏季构图,玫瑰和百合在柔和的背景。美丽的花卉布局。自然的概念。前视图。平躺图片
在白色背景上由美丽的罂粟花组成的创意组合。自然的概念。夏季花卉图案。前视图。平躺图片
基于Android的水稻病虫害图像的识别与诊断系统

网址: Android UI之ImageView图片视图 https://m.huajiangbk.com/newsview546557.html

所属分类:花卉
上一篇: App Inventor移动应用
下一篇: C语言面试题目:把int 直接赋