將以下代碼寫到MulitPointTouchListener.java中,然后對你相應的圖片進行OnTouchListener。
公司主營業(yè)務:網站制作、網站設計、移動網站開發(fā)等業(yè)務。幫助企業(yè)客戶真正實現(xiàn)互聯(lián)網宣傳,提高企業(yè)的競爭能力。成都創(chuàng)新互聯(lián)是一支青春激揚、勤奮敬業(yè)、活力青春激揚、勤奮敬業(yè)、活力澎湃、和諧高效的團隊。公司秉承以“開放、自由、嚴謹、自律”為核心的企業(yè)文化,感謝他們對我們的高要求,感謝他們從不同領域給我們帶來的挑戰(zhàn),讓我們激情的團隊有機會用頭腦與智慧不斷的給客戶帶來驚喜。成都創(chuàng)新互聯(lián)推出南陽免費做網站回饋大家。
例如:imageView.setOnTouchListener(new MulitPointTouchListener ());
在xml中要將ImageView的縮放格式改成Matrix
例如:android:scaleType="matrix"
就可以實現(xiàn)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實現(xiàn)圖形的放大和縮小,可以使用以下代碼:
import java.awt.*;import java.awt.event.*;import javax.swing.*;//實現(xiàn)矩形在規(guī)定時間間隔里循環(huán)放大縮??;
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++語言的各種優(yōu)點,還摒棄了C++里難以理解的多繼承、指針等概念,因此Java語言具有功能強大和簡單易用兩個特征。Java語言作為靜態(tài)面向對象編程語言的代表,極好地實現(xiàn)了面向對象理論,允許程序員以優(yōu)雅的思維方式進行復雜的編程 。
Java具有簡單性、面向對象、分布式、健壯性、安全性、平臺獨立與可移植性、多線程、動態(tài)性等特點。Java可以編寫桌面應用程序、Web應用程序、分布式系統(tǒng)和嵌入式系統(tǒng)應用程序等。
放大圖像不會導致失真,而縮小圖像將不可避免的失真。
Java中也同樣是這樣。
但java提供了4個縮放的微調選項。
image.SCALE_SMOOTH //平滑優(yōu)先
image.SCALE_FAST//速度優(yōu)先
image.SCALE_AREA_AVERAGING //區(qū)域均值
image.SCALE_REPLICATE //像素復制型縮放
image.SCALE_DEFAULT //默認縮放模式
調用方法
Image new_img=old_img.getScaledInstance(1024, 768, Image.SCALE_SMOOTH);
得到一張縮放后的新圖。
在Android開發(fā)中,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系統(tǒng)中的不同組件使用,他們通過Android應用程序框架為開發(fā)人員提供服務。
擴展資料:
Android應用軟件開發(fā)語言有C語言等多種語言,但主流的開發(fā)語言是java語言,這使得界面的功能有了無盡的變化,增加軟件交互的可能性是Java的最大特點。
所有的Android應用程序都是用Java語言編寫的,用java語言開發(fā)的軟件程序庫、數(shù)據庫和運行庫是Android移動軟件的主要特點。
Java語言是增長最快的編程語言,它具有面向對象的特點,它相對容易理解,它的顯著特點是簡單,它繼承了C++語言的高級本質,它是計算機程序設計語言發(fā)展的一大進步,Java語言有一個獨立的體系結構,可以在任何系統(tǒng)中任意運行。
參考資料來源:
百度百科-Android
百度百科-軟件開發(fā)
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實現(xiàn)圖形的放大和縮小,其實就是在畫圖時,改變圖片的長和寬。以下代碼參考一下:
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);
}
}
}
}