目标=把helloworld的文字变成可以缩放的。
测试=需要能实体手机,虚拟机似乎不能进行触摸缩放测试。
版本=android 2.3.3,target=android-10
参考了网上例子。
关键点
=============main.xml
<TextView android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/hello" />
=============A1Activity.java
public void onCreate中,加入:
TextView textView = (TextView) findViewById(R.id.text);
textView.setOnTouchListener(this);
加入public boolean onTouch和相关变量
public boolean onTouch中,最后return true;是关键。
关键文件代码
=============main.xml
<?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/text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/hello" />
</LinearLayout>
=============A1Activity.java
package com.ex.a1205;
import android.app.Activity;
import android.os.Bundle;
import android.util.FloatMath;
import android.util.Log;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.TextView;
public class A1Activity extends Activity implements OnTouchListener {
private static final String TAG = "textZoom";
// We can be in one of these 2 states
static final int NONE = 0;
static final int ZOOM = 1;
int mode = NONE;
static final int MIN_FONT_SIZE = 20;
static final int MAX_FONT_SIZE = 150;
float oldDist = 1f;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView textView = (TextView) findViewById(R.id.text);
textView.setOnTouchListener(this);
}
public boolean onTouch(View v, MotionEvent event) {
TextView textView = (TextView) findViewById(R.id.text);
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_POINTER_DOWN:
oldDist = spacing(event);
Log.d(TAG, "oldDist=" + oldDist);
if (oldDist > 10f) {
mode = ZOOM;
}
break;
case MotionEvent.ACTION_POINTER_UP:
mode = NONE;
break;
case MotionEvent.ACTION_MOVE:
if (mode == ZOOM) {
float newDist = spacing(event);
if (newDist > 10f) {
float scale = newDist / oldDist;
if (scale > 1) {
scale = 1.1f;
} else if (scale < 1) {
scale = 0.95f;
}
float currentSize = textView.getTextSize() * scale;
if ((currentSize < MAX_FONT_SIZE && currentSize > MIN_FONT_SIZE)
|| (currentSize >= MAX_FONT_SIZE && scale < 1)
|| (currentSize <= MIN_FONT_SIZE && scale > 1)) {
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, currentSize);
}
}
}
break;
}
return true;
}
/** Determine the space between the first two fingers */
private float spacing(MotionEvent event) {
float x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);
return FloatMath.sqrt(x * x + y * y);
}
}
执行结果
似曾相识
- 大小: 53 KB
- 大小: 2.7 KB
- 大小: 6.8 KB
分享到:
相关推荐
A scrollable, zoomable, and scalable picture box
CommentGallery 利用Fresco实现的一个Android图像库
Zoomy is an easy to use pinch-to-zoom Android library Installation Zoomy is available in the JCenter, so you just need to add it as a dependency compile 'com.ablanco.zoomy:zoomy:{latest version}' ...
以鼠标指针为中心缩放图片以及ROI的框选
查看示例: () () ()快速开始import zoomable from 'd3-zoomable';或者const zoomable = require('d3-zoomable');甚至[removed][removed]然后const myZoom = zoomable();myZoom() .svgEl(<SVG eleme
Android: TouchImageView Created by: Mike Ortiz Contributions by: Patrick Lackemacher Babay88 @ipsilondev hank-cp singpolyma Capabilities: TouchImageView extends ImageView and supports all of ...
A scrollable, zoomable, and scalable picture box
可缩放 拍一张小照片并放大! 使用 jQuery 的 JavaScript。 合作者:
react-native-zoomable-view 一种视图组件,可通过捏合缩放,点击移动和双击缩放来进行React。 您可以缩放所有内容,包括普通图像,文本和更复杂的嵌套视图。 我们已经在两个项目中的生产中使用了该组件,但是出于...
适用于Android的可缩放ImageView可以在AdapterView和Recyclerview中使用。一个可拉伸拉伸的ImageView,可在AdapterView和RecyclerView中使用。 特征 使用多点触控进行缩放。 可以在AdapterView和Recyclerview中...
ZVTM 是一个用 Java 实现的可缩放用户界面 (ZUI) 工具包。 它简化了创建复杂的可视化多尺度编辑器的任务,其中必须显示和动画大量对象。
Supported Android versions API 22 and higher Earlier versions are to be added soon Supported technologies Databinding Livedata Features Collapsing toolbar Zoomable photo image Tabs pager Option ...
Zoomable.js Zoomable很小(最小3k),没有依赖项,易于使用,图像不会破坏上下文。 使用Zoomable,用户无需打开新的浏览器标签即可查看自己的那些大而漂亮的图片。 Zoomable是一种灯箱,灵感来自Medium.com上看到的...
Elementary is a simple Periodic Table and element reference application for Android. Also available for Windows 10 Features Zoomable Periodic Table of the Elements Tap an element to view some basic...
With Xia Express app, you can create interactive images by croping zoomable details. You can add commentaries to those details. This Android application is a lite version of Xia on desktop only for ...
可观察+创建React应用 该示例已移至
Zoomable(state = state) { Text(text = "Zoom me!") } 执照 MIT License Copyright (c) 2021 Tlaster Permission is hereby granted, free of charge, to any person obtaining a copy of this software and ass
可缩放这是使用 Jack Moore 的 svg 和的可缩放矢量图的基本实现。包含的 .svg 文件是波士顿 ICA 的原始重绘。 导出 .svg 的最佳方式是通过 Adobe Illustrator、“另存为”、“.svg”
CommentGallery This project contains serval librarys for demonstrating image gallery with Fresco's ZoomableDraweeView(Use the optimized version I made), such as image grid and ...1. Usage of Zoomable
React可缩放媒体 React组件以处理图像,视频和div元素的缩放 演示页面 快速入门命令 yarn start yarn demo 去做 记录用法 添加种植面积 在Codesandbox中添加示例代码 添加对DIV元素的支持 执照