新网创想网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
1.在Java Code定义动画
苏尼特右网站建设公司成都创新互联,苏尼特右网站设计制作,有大型网站制作公司丰富经验。已为苏尼特右上1000家提供企业网站建设服务。企业网站搭建\外贸网站建设要多少钱,请找那个售后服务好的苏尼特右做网站的公司定做!
AlphaAnimation
渐变透明度动画效果
ScaleAnimation
渐变尺寸伸缩动画效果
TranslateAnimation
画面转换位置移动动画效果
RotateAnimation
画面转移旋转动画效果
//定义变量
private Animation myAnimation_Alpha;
private Animation myAnimation_Scale;
private Animation myAnimation_Translate;
private Animation myAnimation_Rotate;
myAnimation_Alpha=new AlphaAnimation(0.1f, 1.0f);
//第一个参数fromAlpha为 动画开始时候透明度
//第二个参数toAlpha为 动画结束时候透明度
myAnimation_Scale =new ScaleAnimation(0.0f, 1.4f, 0.0f, 1.4f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
//第一个参数fromX为动画起始时 X坐标上的伸缩尺寸
//第二个参数toX为动画结束时 X坐标上的伸缩尺寸
//第三个参数fromY为动画起始时Y坐标上的伸缩尺寸
//第四个参数toY为动画结束时Y坐标上的伸缩尺寸
//第五个参数pivotXType为动画在X轴相对于物件位置类型
//第六个参数pivotXValue为动画相对于物件的X坐标的开始位置
//第七个参数pivotXType为动画在Y轴相对于物件位置类型
//第八个参数pivotYValue为动画相对于物件的Y坐标的开始位置
myAnimation_Translate=new TranslateAnimation(30.0f, -80.0f, 30.0f, 300.0f);
//第一个参数fromXDelta为动画起始时 X坐标上的移动位置
//第二个参数toXDelta为动画结束时 X坐标上的移动位置
//第三个参数fromYDelta为动画起始时Y坐标上的移动位置
//第四个参数toYDelta为动画结束时Y坐标上的移动位置
myAnimation_Rotate=new RotateAnimation(0.0f,350.0f,Animation.RELATIVE_TO_SELF,0.5f, Animation.RELATIV E_TO_SELF, 0.5f);
//第一个参数fromDegrees为动画起始时的旋转角度
//第二个参数toDegrees为动画旋转到的角度
//第三个参数pivotXType为动画在X轴相对于物件位置类型
//第四个参数pivotXValue为动画相对于物件的X坐标的开始位
//第五个参数pivotXType为动画在Y轴相对于物件位置类型
//第六个参数pivotYValue为动画相对于物件的Y坐标的开始位置
2.3自定义动画
除了以上4类动画外,在代码中继承android.view.animation.Animation类来实现自定义效果。通过重写Animation的applyTransformation(float interpolatedTime, Transformation t)函数来实现自定义动画效果。
在绘制动画的过程中会反复的调用applyTransformation函数,每次调用参数interpolatedTime值都会变化,该参数从0渐 变为1,当该参数为1时表明动画结束。通过参数Transformation 来获取变换的矩阵(matrix),通过改变矩阵就可以实现各种复杂的效果。
自定义图片旋转效果动画代码。
class ViewAnimation extends Animation
{
int mCenterX;
int mCenterY;
Camera camera = new Camera();
public ViewAnimation()
{
}
@Override
public void initialize(int width, int height, int parentWidth,
int parentHeight)
{
super.initialize(width, height, parentWidth, parentHeight);
mCenterX = width/2;
mCenterY = height/2;
setDuration(2500);
setFillAfter(true);
// setRepeatCount(100);
setInterpolator(new LinearInterpolator());
}
@Override
protected void applyTransformation(float interpolatedTime,
Transformation t)
{
final Matrix matrix = t.getMatrix();
camera.save();
camera.translate(0.0f, 0.0f, (1300 - 1300.0f * interpolatedTime));
camera.rotateY(360 * interpolatedTime);
camera.getMatrix(matrix);
matrix.preTranslate(-mCenterX, -mCenterY);
matrix.postTranslate(mCenterX, mCenterY);
camera.restore();
}
}
camera.translate(0.0f, 0.0f, (1300 - 1300.0f * interpolatedTime))在第一次调用的时候interpolatedTime值为0,相当于把ImageView在Z轴后移1300像素, 然后逐步的往前移动到0,同时camera.rotateY(360 * interpolatedTime)函数又把ImageView沿Y轴翻转360度
2.4动画实现
在代码的一个activity中,将需要动画的控件与已定义的动画关联,开启动画
private Animation mAnimationXXX = null;//生明动画
ImageView mAnimImageX = null;//生明 ImageView控件(或者其它需要有动画的控件)
mAnimImageX = (ImageView) this.findViewById(R.id.anim_imageX);//取得图片资源
mAnimImageX.startAnimation(mAnimationXXX);//基于xml文件,开启动画
mAnimImageX.startAnimation(new ViewAnimation());//基于自定义控件,开启动画。
2.5图片旋转Demo
public class AnimationDemo extends Activity {
/** Called when the activity is first created. */
ImageView mAnimImage;
private Animation mAnimationAlpha = null;
private Animation mAnimationScale = null;
private Animation mAnimationTranslate = null;
private Animation mAnimationRotate = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mAnimImage = (ImageView) this.findViewById(R.id.anim_image1);
mAnimationAlpha = AnimationUtils.loadAnimation(this,R.anim.alpha_animation);
mAnimationScale = AnimationUtils.loadAnimation(this,R.anim.scale_animation);
mAnimationTranslate = AnimationUtils.loadAnimation(this,R.anim.translate_animation);
mAnimationRotate = AnimationUtils.loadAnimation(this,R.anim.rotate_animation);
}
public boolean onKeyUp(int keyCode, KeyEvent event)
{
switch ( keyCode )
{
case KeyEvent.KEYCODE_1:
mAnimImage.startAnimation(mAnimationAlpha);
break;
case KeyEvent.KEYCODE_2:
mAnimImage.startAnimation(mAnimationScale);
break;
case KeyEvent.KEYCODE_3:
mAnimImage.startAnimation(mAnimationTranslate);
break;
case KeyEvent.KEYCODE_4:
mAnimImage.startAnimation( mAnimationRotate);
break;
case KeyEvent.KEYCODE_5:
mAnimImage.startAnimation(new ViewAnimation());
break;
}
return true;
}
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ( keyCode == KeyEvent.KEYCODE_BACK)
{
this.finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
3 Frame动画
Frame 动画是顺序播放事先做好的图片。可以在XML Resource定义,也可以使AnimationDrawable中的API定义。
3.1Frame动画实现
定义XML文件
?xml version="1.0" encoding="utf-8"?
animation-list xmlns:android=""
android:oneshot="false"
item android:drawable="@drawable/a1" android:duration="500" /
item android:drawable="@drawable/a2" android:duration="500" /
item android:drawable="@drawable/a3" android:duration="500" /
item android:drawable="@drawable/a4" android:duration="500" /
item android:drawable="@drawable/a5" android:duration="500" /
item android:drawable="@drawable/a6" android:duration="500" /
item android:drawable="@drawable/a7" android:duration="500" /
item android:drawable="@drawable/a8" android:duration="500" /
item android:drawable="@drawable/a9" android:duration="500" /
item android:drawable="@drawable/a10" android:duration="500" /
item android:drawable="@drawable/a11" android:duration="500" /
item android:drawable="@drawable/a12" android:duration="500" /
item android:drawable="@drawable/a13" android:duration="500" /
item android:drawable="@drawable/a14" android:duration="500" /
item android:drawable="@drawable/a15" android:duration="500" /
/animation-list
定义动画类
public class GameView extends View
{
private AnimationDrawable frameAnimation = null;
Context mContext = null;
public GameView(Context context)
{
super(context);
mContext = context;
ImageView img = new ImageView(mContext);
img.setBackgroundResource(R.anim.frameanimation);
frameAnimation = (AnimationDrawable) img.getBackground();
frameAnimation.setOneShot( false );
this.setBackgroundDrawable(frameAnimation);
}
public void onDraw(Canvas canvas)
{
super.onDraw(canvas);
}
public boolean onKeyUp(int keyCode, KeyEvent event)
{
switch ( keyCode )
{
case KeyEvent.KEYCODE_DPAD_UP:
frameAnimation.start();
break;
}
return true;
}
}
开启动画
public class Activity01 extends Activity
{
private GameView mGameView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
mGameView = new GameView(this);
setContentView(mGameView);
}
public boolean onKeyUp(int keyCode, KeyEvent event)
{
if ( mGameView == null )
{
return false;
}
mGameView.onKeyUp(keyCode,event);
return true;
}
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ( mGameView == null )
{
return false;
}
if ( keyCode == KeyEvent.KEYCODE_BACK)
{
this.finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
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);
}
}
}
}
放大图像不会导致失真,而缩小图像将不可避免的失真。
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);
得到一张缩放后的新图。
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class TestImage extends Frame
{
private static final long serialVersionUID = 1L;
private static boolean PRESSED = false;
private static int pointX = 0;
private static int pointy = 200;
private static int RIGHT_GO = 0;
private static int LEFT_GO = 0;
private static int DIR = 0;
private static int ANGLE = 0;
private static int W = 50;
private static int H = 60;
private _Canvas canvas = null;
public TestImage ()
{
add (canvas = new _Canvas ());
setIgnoreRepaint (true);
requestFocus ();
}
public class _Canvas extends Canvas implements Runnable
{
private static final long serialVersionUID = 1L;
private BufferedImage bi = null;
private Image bufferedImage = null;
private Thread thread = null;
private long sleepTime = 10;
public _Canvas ()
{
try
{
bi = ImageIO.read (new File ("go.png"));
}
catch (IOException e)
{}
setBackground (Color.BLACK);
requestFocus ();
addKeyListener (new KeyListener ()
{
@Override
public void keyTyped ( KeyEvent e )
{}
@Override
public void keyReleased ( KeyEvent e )
{
RIGHT_GO = 0;
PRESSED = false;
}
@Override
public void keyPressed ( KeyEvent e )
{
// 38 40 37 39上下左右
DIR = e.getKeyCode ();
PRESSED = true;
}
});
}
@Override
public void paint ( Graphics g )
{
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint (RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2d.drawImage (rotateImage (bi.getSubimage (RIGHT_GO, LEFT_GO, W, H), ANGLE, true), pointX, pointy, W, H,
this);
g2d.dispose ();
}
@Override
public void update ( Graphics g )
{
if (null == bufferedImage)
{
bufferedImage = createImage (getWidth (), getHeight ());
}
Graphics bufferedG = bufferedImage.getGraphics ();
bufferedG.clearRect (0, 0, getWidth (), getHeight ());
paint (bufferedG);
bufferedG.dispose ();
g.drawImage (bufferedImage, 0, 0, this);
g.dispose ();
}
public void start ()
{
thread = new Thread (this);
thread.setName ("TestImage");
thread.setPriority (Thread.MIN_PRIORITY);
thread.start ();
}
public synchronized void stop ()
{
thread = null;
notify ();
}
@Override
public void run ()
{
Thread me = Thread.currentThread ();
while (thread == me !isShowing () || getSize ().width == 0)
{
try
{
Thread.sleep (555);
}
catch (InterruptedException e)
{
return;
}
}
while (thread == me isShowing ())
{
if (PRESSED)
{
try
{
if (DIR == 39)
{
RIGHT_GO = RIGHT_GO + 50;
LEFT_GO = 0;
pointX = pointX + 1;
if (pointX 420)
{
ANGLE = 90;
pointX--;
pointy--;
W = 60;
H = 50;
}
if (RIGHT_GO 50)
{
RIGHT_GO = 0;
}
}
else if (DIR == 37)
{
pointX = pointX - 1;
RIGHT_GO = RIGHT_GO + 50;
LEFT_GO = 60;
if (pointX 0)
{
ANGLE = -90;
pointX++;
pointy--;
W = 60;
H = 50;
}
if (RIGHT_GO 50)
{
RIGHT_GO = 0;
}
}
else if (DIR == 38)
{
W = 50;
H = 60;
pointy = 150;
ANGLE = 0;
RIGHT_GO = 100;
}
else if (DIR == 40)
{
W = 50;
H = 60;
ANGLE = 0;
pointy = 200;
RIGHT_GO = 0;
}
Thread.sleep (sleepTime);
repaint ();
}
catch (InterruptedException e)
{
break;
}
}
else
{
RIGHT_GO = RIGHT_GO + 50;
LEFT_GO = 0;
pointX = pointX + 1;
if (RIGHT_GO 50)
{
RIGHT_GO = 0;
}
if (pointX 500)
{
pointX = 0;
}
try
{
Thread.sleep (sleepTime);
repaint ();
}
catch (InterruptedException e)
{
break;
}
}
}
thread = null;
}
}
/**
* 旋转图像为指定角度
*
* @param degree
* @return
*/
public static BufferedImage rotateImage ( final BufferedImage image, final int angdeg, final boolean d )
{
int w = image.getWidth ();
int h = image.getHeight ();
int type = image.getColorModel ().getTransparency ();
BufferedImage img;
Graphics2D graphics2d;
( graphics2d = ( img = new BufferedImage (w, h, type) ).createGraphics () ).setRenderingHint (
RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2d.rotate (d ? -Math.toRadians (angdeg) : Math.toRadians (angdeg), w / 2, h / 2);
graphics2d.drawImage (image, 0, 0, null);
graphics2d.dispose ();
return img;
}
public static void main ( String[] args )
{
EventQueue.invokeLater (new Runnable ()
{
@Override
public void run ()
{
final TestImage ti = new TestImage ();
ti.setSize (new Dimension (500, 300));
ti.setLocationRelativeTo (null);
ti.addWindowListener (new WindowAdapter ()
{
@Override
public void windowClosing ( WindowEvent e )
{
System.exit (0);
}
@Override
public void windowDeiconified ( WindowEvent e )
{
ti.canvas.start ();
}
@Override
public void windowIconified ( WindowEvent e )
{
ti.canvas.stop ();
}
});
ti.setResizable (false);
ti.canvas.start ();
ti.setVisible (true);
}
});
}
}
这个只是实现了移动,你参考以下吧 !
public class MoveImage {
static int x,y;
private static int num=0;
private static Icon icon=null;
public static void main(String[] args) throws Exception{
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().setLayout(null);//这个要设置成 null
//图片
icon = new ImageIcon("d:/test.gif");//d:/test.gif本地一张图片
JLabel l = new JLabel(icon); //创建具有指定图像的 JLabel 实例。
l.setSize(icon.getIconWidth(),icon.getIconHeight());//设置面板的宽度和高度
l.setBorder(BorderFactory.createLineBorder(Color.red));//给图片加上红色外框
f.getContentPane().add(l);
f.setSize(900,700);
f.setVisible(true);
l.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
x=e.getX();
y=e.getY();
}
});
l.addMouseMotionListener(new MouseMotionListener(){
public void mouseDragged(MouseEvent e) {
JLabel l = (JLabel)e.getSource();
l.setLocation(l.getX()+e.getX()-x,l.getY()+e.getY()-y);
}
public void mouseMoved(MouseEvent e) {}
});
}
要用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应用程序、分布式系统和嵌入式系统应用程序等。