十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
将以下代码写到MulitPointTouchListener.java中,然后对你相应的图片进行OnTouchListener。
公司主营业务:网站制作、网站设计、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。成都创新互联是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。成都创新互联推出南阳免费做网站回馈大家。
例如:imageView.setOnTouchListener(new MulitPointTouchListener ());
在xml中要将ImageView的缩放格式改成Matrix
例如:android:scaleType="matrix"
就可以实现android界面整体缩放
下面是MulitPointTouchListener.java代码:public class MulitPointTouchListener implements OnTouchListener { private static final String TAG = "Touch"; // These matrices will be used to move and zoom image Matrix matrix = new Matrix(); Matrix savedMatrix = new Matrix(); // We can be in one of these 3 states static final int NONE = 0; static final int DRAG = 1; static final int ZOOM = 2; int mode = NONE; // Remember some things for zooming PointF start = new PointF(); PointF mid = new PointF(); float oldDist = 1f; @Override public boolean onTouch(View v, MotionEvent event) { ImageView view = (ImageView) v; // Log.e("view_width", // view.getImageMatrix()..toString()+"*"+v.getWidth()); // Dump touch event to log dumpEvent(event); // Handle touch events here... switch (event.getAction() MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: matrix.set(view.getImageMatrix()); savedMatrix.set(matrix); start.set(event.getX(), event.getY()); //Log.d(TAG, "mode=DRAG"); mode = DRAG; //Log.d(TAG, "mode=NONE"); break; case MotionEvent.ACTION_POINTER_DOWN: oldDist = spacing(event); //Log.d(TAG, "oldDist=" + oldDist); if (oldDist 10f) { savedMatrix.set(matrix); midPoint(mid, event); mode = ZOOM; //Log.d(TAG, "mode=ZOOM"); } break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_POINTER_UP: mode = NONE; //Log.e("view.getWidth", view.getWidth() + ""); //Log.e("view.getHeight", view.getHeight() + ""); break; case MotionEvent.ACTION_MOVE: if (mode == DRAG) { // ... matrix.set(savedMatrix); matrix.postTranslate(event.getX() - start.x, event.getY() - start.y); } else if (mode == ZOOM) { float newDist = spacing(event); //Log.d(TAG, "newDist=" + newDist); if (newDist 10f) { matrix.set(savedMatrix); float scale = newDist / oldDist; matrix.postScale(scale, scale, mid.x, mid.y); } } break; } view.setImageMatrix(matrix); return true; // indicate event was handled } private void dumpEvent(MotionEvent event) { String names[] = { "DOWN", "UP", "MOVE", "CANCEL", "OUTSIDE", "POINTER_DOWN", "POINTER_UP", "7?", "8?", "9?" }; StringBuilder sb = new StringBuilder(); int action = event.getAction(); int actionCode = action MotionEvent.ACTION_MASK; sb.append("event ACTION_").append(names[actionCode]); if (actionCode == MotionEvent.ACTION_POINTER_DOWN || actionCode == MotionEvent.ACTION_POINTER_UP) { sb.append("(pid ").append( action MotionEvent.ACTION_POINTER_ID_SHIFT); sb.append(")"); } sb.append("["); for (int i = 0; i event.getPointerCount(); i++) { sb.append("#").append(i); sb.append("(pid ").append(event.getPointerId(i)); sb.append(")=").append((int) event.getX(i)); sb.append(",").append((int) event.getY(i)); if (i + 1 event.getPointerCount()) sb.append(";"); } sb.append("]"); //Log.d(TAG, sb.toString()); } 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); } private void midPoint(PointF point, MotionEvent event) { float x = event.getX(0) + event.getX(1); float y = event.getY(0) + event.getY(1); point.set(x / 2, y / 2); } }
要用Java实现图形的放大和缩小,可以使用以下代码:
import java.awt.*;import java.awt.event.*;import javax.swing.*;//实现矩形在规定时间间隔里循环放大缩小;
public class Test02 extends JFrame implements Runnable{ static int w = 0,h = 0; //w:矩形宽度,h:矩形高度 ;
boolean flag = false;//false:放大,true:缩小 public Test02(){ this.setSize (500,500); this.setVisible (true); this.setTitle (w+","+h); this.setLocationRelativeTo (this); this.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); } public void paint(Graphics g){ int width = getWidth();
//窗口宽度 int height = getWidth(); //窗口高度 g.setColor (Color.WHITE); //设置画笔颜色 g.fillRect (0,0,width,height); //填充窗口 int x = (width-w)/2; //x:矩形左上角横坐标 int y = (height-h)/2; //y:矩形左上角纵坐标 g.setColor (Color.BLUE); //同上... g.drawRect (x,y,w,h);
//画矩形,实心矩形为fillRect(....) g.setColor (Color.RED); //同上... g.drawOval (x,y,w,h); //画椭圆 setTitle (w+","+h); //以矩形宽和高来设置窗口标题 } public void setSize(){ float n = getWidth()/getWidth(); if(w==0||h==0) flag = false;
if(w==getWidth()||h==getWidth()) flag = true; if(!flag){ w+=1; h+=n; } if(flag){ w-=1; h-=n; } } public void run(){ while(true){ try{ Thread.sleep (5);//间隔 }catch(InterruptedException ie){} setSize(); repaint(); } } public static void main(String[] args){ Test02 t = new Test02(); Thread th = new Thread(t); th.start(); }}
Java是一门面向对象编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承、指针等概念,因此Java语言具有功能强大和简单易用两个特征。Java语言作为静态面向对象编程语言的代表,极好地实现了面向对象理论,允许程序员以优雅的思维方式进行复杂的编程 。
Java具有简单性、面向对象、分布式、健壮性、安全性、平台独立与可移植性、多线程、动态性等特点。Java可以编写桌面应用程序、Web应用程序、分布式系统和嵌入式系统应用程序等。
放大图像不会导致失真,而缩小图像将不可避免的失真。
Java中也同样是这样。
但java提供了4个缩放的微调选项。
image.SCALE_SMOOTH //平滑优先
image.SCALE_FAST//速度优先
image.SCALE_AREA_AVERAGING //区域均值
image.SCALE_REPLICATE //像素复制型缩放
image.SCALE_DEFAULT //默认缩放模式
调用方法
Image new_img=old_img.getScaledInstance(1024, 768, Image.SCALE_SMOOTH);
得到一张缩放后的新图。
在Android开发中,java代码用于将zoom设置为fitxy,从而使类能够继承uibutton、重写button和设置ImageView size来设置imagebutton的大小,具体Java设置代码如下:
ImageButton
android:id="@+id/btn_delete"
android:layout_width="48dp"
android:layout_height="80dp"
android:scaleType="fitXY"
android:background="@drawable/bg_sms"/
此外,Android还包括一些C/C++库,这些库可以被Android系统中的不同组件使用,他们通过Android应用程序框架为开发人员提供服务。
扩展资料:
Android应用软件开发语言有C语言等多种语言,但主流的开发语言是java语言,这使得界面的功能有了无尽的变化,增加软件交互的可能性是Java的最大特点。
所有的Android应用程序都是用Java语言编写的,用java语言开发的软件程序库、数据库和运行库是Android移动软件的主要特点。
Java语言是增长最快的编程语言,它具有面向对象的特点,它相对容易理解,它的显著特点是简单,它继承了C++语言的高级本质,它是计算机程序设计语言发展的一大进步,Java语言有一个独立的体系结构,可以在任何系统中任意运行。
参考资料来源:
百度百科-Android
百度百科-软件开发
import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.AffineTransform;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import javax.swing.filechooser.FileNameExtensionFilter;
/**
* 放大与缩小
*/
public class ImageFrame extends JFrame {
private static final long serialVersionUID = -3297158451411015547L;
private Canvas canvas;
private JButton smaller;
private JButton bigger;
private float scale = 1f;
public ImageFrame() {
super();
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"JPG GIF Images", "jpg", "gif");
// 设置文件类型
chooser.setFileFilter(filter);
// 打开选择器面板
int returnVal = chooser.showOpenDialog(new JPanel());
if (returnVal == JFileChooser.APPROVE_OPTION) {
initGUI((chooser.getSelectedFile().getAbsolutePath()));
}
}
Image image;
private void initGUI(String imgPath) {
try {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
getContentPane().setLayout(null);
{
Toolkit tk = getToolkit();
image = tk.getImage(imgPath);
// 这里的地址填上你图片的位置
}
{
canvas = new Canvas() {
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
AffineTransform at = new AffineTransform();
at.scale(scale, scale);
g2d.setTransform(at);
g2d.drawImage(image, at, ImageFrame.this);
}
};
getContentPane().add(canvas);
canvas.setBounds(0, 0, 500, 514);
}
{
bigger = new JButton();
getContentPane().add(bigger);
bigger.setText("放 大");
bigger.setBounds(79, 526, 109, 28);
bigger.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (scale 5)
scale += 0.1;
canvas.repaint();
}
});
}
{
smaller = new JButton();
getContentPane().add(smaller);
smaller.setText("缩 小");
smaller.setBounds(310, 526, 109, 28);
smaller.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (scale 0.5)
scale -= 0.1;
canvas.repaint();
}
});
}
pack();
setSize(510, 600);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
ImageFrame inst = new ImageFrame();
inst.setLocationRelativeTo(null);
inst.setVisible(true);
inst.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
}
}
java实现图形的放大和缩小,其实就是在画图时,改变图片的长和宽。以下代码参考一下:
import java.awt.Graphics;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.filechooser.FileNameExtensionFilter;
public class App extends JFrame implements MouseListener, ActionListener {
int x = 0;
int y = 0;
File[] selectedFiles = null;
int fileIndex = 0;
int width = 200;
int height = 200;
public App() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setSize(400, 300);
setResizable(false);
getContentPane().setLayout(null);
JPanel panel = new ImagePanel();
panel.setBounds(12, 40, 370, 218);
getContentPane().add(panel);
addMouseListener(this);
JButton btnBrowse = new JButton("Browse");
btnBrowse.addActionListener(this);
btnBrowse.setBounds(12, 9, 91, 21);
getContentPane().add(btnBrowse);
setVisible(true);
}
public static void main(String[] args) {
new App();
}
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser();
chooser.setMultiSelectionEnabled(true);
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"JPG GIF Images", "jpg", "gif");
// 设置文件类型
chooser.setFileFilter(filter);
// 打开选择器面板
int returnVal = chooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
selectedFiles = chooser.getSelectedFiles();
repaint();
}
}
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
Point point = MouseInfo.getPointerInfo().getLocation();
x = point.x;
y = point.y;
}
public void mouseReleased(MouseEvent e) {
Point point = MouseInfo.getPointerInfo().getLocation();
int thisX = point.x;
int thisY = point.y;
System.out.println("thisX=" + thisX + " " + "thisY=" + thisY);
if ((y - thisY 20 y - thisY 0)
|| (y - thisY 0 y - thisY -20)) {
// Y 在20范围内移动认为是水平移动
if (x thisX) {
// right
if (selectedFiles != null
fileIndex selectedFiles.length - 1) {
fileIndex++;
}
} else {
// left
if (selectedFiles != null fileIndex 0) {
fileIndex--;
}
}
} else {
if (x thisX) {
// 右下
width += 20;
height += 20;
} else {
// 左上
width -= 20;
height -= 20;
}
}
repaint();
}
class ImagePanel extends JPanel {
public void paint(Graphics g) {
super.paint(g);
if (selectedFiles != null) {
ImageIcon icon = new ImageIcon(selectedFiles[fileIndex]
.getPath());
g.drawImage(icon.getImage(), 0, 0, width, height, this);
}
}
}
}