Java 安卓-随机图片动画
我希望动画在从图片池中拾取图像后开始。我以为我可以用一个随机选择的开关来做,但后来我得到了一个错误,因为“球”无法解决。我希望这不是完全错误,但我也不知道如何修复它Java 安卓-随机图片动画,java,android,animation,graphics,Java,Android,Animation,Graphics,我希望动画在从图片池中拾取图像后开始。我以为我可以用一个随机选择的开关来做,但后来我得到了一个错误,因为“球”无法解决。我希望这不是完全错误,但我也不知道如何修复它 protected void onDraw(Canvas c) { Random randLan = new Random(); int imag = randLan.nextInt(4) + 1; int image = 0; switch (imag) {
protected void onDraw(Canvas c) {
Random randLan = new Random();
int imag = randLan.nextInt(4) + 1;
int image = 0;
switch (imag)
{
case 1:
image = R.drawable.ballgel;
BitmapDrawable ball = (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.ballgel);
break;
case 2:
image = R.drawable.ballgel;
BitmapDrawable ball = (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.ballbl);
break;
case 3:
image = R.drawable.ballgel;
BitmapDrawable ball = (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.ballgrue);
break;
case 4:
image = R.drawable.ballgel;
BitmapDrawable ball = (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.ballrot);
break;
}
if (x<0) {
x = this.getWidth()/2;
y = this.getHeight()/3;
}
else {
x += xVelocity;
if ((x > this.getWidth() - ball.getBitmap().getWidth()) || (x < 0)) {
boolean continueAnimation = false;
}
}
c.drawBitmap(ball.getBitmap(), x, y, null);
if(continueAnimation)
{
h.postDelayed(r, FRAME_RATE);
}
else {
x = this.getWidth()-ball.getBitmap().getWidth();
}
}
protectedvoid onDraw(Canvas c){
Random randLan=新的Random();
int imag=randLan.nextInt(4)+1;
int image=0;
开关(imag)
{
案例1:
图像=R.drawable.ballgel;
BitmapDrawable ball=(BitmapDrawable)mContext.getResources().getDrawable(R.drawable.ballgel);
打破
案例2:
图像=R.drawable.ballgel;
BitmapDrawable ball=(BitmapDrawable)mContext.getResources().getDrawable(R.drawable.ballbl);
打破
案例3:
图像=R.drawable.ballgel;
BitmapDrawable ball=(BitmapDrawable)mContext.getResources().getDrawable(R.drawable.ballgrue);
打破
案例4:
图像=R.drawable.ballgel;
BitmapDrawable ball=(BitmapDrawable)mContext.getResources().getDrawable(R.drawable.ballrot);
打破
}
如果(x this.getWidth()-ball.getBitmap().getWidth())| |(x<0)){
布尔continueAnimation=false;
}
}
c、 drawBitmap(ball.getBitmap(),x,y,null);
if(continueAnimation)
{
h、 后延迟(r,帧速率);
}
否则{
x=this.getWidth()-ball.getBitmap().getWidth();
}
}
你应该在开头加上:
private BitmapDrawable ball;
然后将开关中的位图可绘制球
替换为球
。
与此类似,ball
在开始时初始化,并在开关中指定
我是为你做的:
private BitmapDrawable ball;
protected void onDraw(Canvas c) {
Random randLan = new Random();
int imag = randLan.nextInt(4) + 1;
int image = 0;
switch (imag)
{
case 1:
image = R.drawable.ballgel;
ball = (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.ballgel);
break;
case 2:
image = R.drawable.ballgel;
ball = (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.ballbl);
break;
case 3:
image = R.drawable.ballgel;
ball = (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.ballgrue);
break;
case 4:
image = R.drawable.ballgel;
ball = (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.ballrot);
break;
}
if (x<0) {
x = this.getWidth()/2;
y = this.getHeight()/3;
}
else {
x += xVelocity;
if ((x > this.getWidth() - ball.getBitmap().getWidth()) || (x < 0)) {
boolean continueAnimation = false;
}
}
c.drawBitmap(ball.getBitmap(), x, y, null);
if(continueAnimation)
{
h.postDelayed(r, FRAME_RATE);
}
else {
x = this.getWidth()-ball.getBitmap().getWidth();
}
}
私人位图可绘制球;
受保护的空白onDraw(画布c){
Random randLan=新的Random();
int imag=randLan.nextInt(4)+1;
int image=0;
开关(imag)
{
案例1:
图像=R.drawable.ballgel;
ball=(BitmapDrawable)mContext.getResources().getDrawable(R.drawable.ballgel);
打破
案例2:
图像=R.drawable.ballgel;
ball=(BitmapDrawable)mContext.getResources().getDrawable(R.drawable.ballbl);
打破
案例3:
图像=R.drawable.ballgel;
ball=(BitmapDrawable)mContext.getResources().getDrawable(R.drawable.ballgrue);
打破
案例4:
图像=R.drawable.ballgel;
ball=(BitmapDrawable)mContext.getResources().getDrawable(R.drawable.ballrot);
打破
}
如果(x this.getWidth()-ball.getBitmap().getWidth())| |(x<0)){
布尔continueAnimation=false;
}
}
c、 drawBitmap(ball.getBitmap(),x,y,null);
if(continueAnimation)
{
h、 后延迟(r,帧速率);
}
否则{
x=this.getWidth()-ball.getBitmap().getWidth();
}
}
你应该在开头加上:
private BitmapDrawable ball;
然后将开关中的位图可绘制球
替换为球
。
与此类似,ball
在开始时初始化,并在开关中指定
我是为你做的:
private BitmapDrawable ball;
protected void onDraw(Canvas c) {
Random randLan = new Random();
int imag = randLan.nextInt(4) + 1;
int image = 0;
switch (imag)
{
case 1:
image = R.drawable.ballgel;
ball = (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.ballgel);
break;
case 2:
image = R.drawable.ballgel;
ball = (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.ballbl);
break;
case 3:
image = R.drawable.ballgel;
ball = (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.ballgrue);
break;
case 4:
image = R.drawable.ballgel;
ball = (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.ballrot);
break;
}
if (x<0) {
x = this.getWidth()/2;
y = this.getHeight()/3;
}
else {
x += xVelocity;
if ((x > this.getWidth() - ball.getBitmap().getWidth()) || (x < 0)) {
boolean continueAnimation = false;
}
}
c.drawBitmap(ball.getBitmap(), x, y, null);
if(continueAnimation)
{
h.postDelayed(r, FRAME_RATE);
}
else {
x = this.getWidth()-ball.getBitmap().getWidth();
}
}
私人位图可绘制球;
受保护的空白onDraw(画布c){
Random randLan=新的Random();
int imag=randLan.nextInt(4)+1;
int image=0;
开关(imag)
{
案例1:
图像=R.drawable.ballgel;
ball=(BitmapDrawable)mContext.getResources().getDrawable(R.drawable.ballgel);
打破
案例2:
图像=R.drawable.ballgel;
ball=(BitmapDrawable)mContext.getResources().getDrawable(R.drawable.ballbl);
打破
案例3:
图像=R.drawable.ballgel;
ball=(BitmapDrawable)mContext.getResources().getDrawable(R.drawable.ballgrue);
打破
案例4:
图像=R.drawable.ballgel;
ball=(BitmapDrawable)mContext.getResources().getDrawable(R.drawable.ballrot);
打破
}
如果(x this.getWidth()-ball.getBitmap().getWidth())| |(x<0)){
布尔continueAnimation=false;
}
}
c、 drawBitmap(ball.getBitmap(),x,y,null);
if(continueAnimation)
{
h、 后延迟(r,帧速率);
}
否则{
x=this.getWidth()-ball.getBitmap().getWidth();
}
}
将一些图像放入您的绘图中
然后在drawble name.xml中创建一个xml文件
使用此代码:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/a"
android:duration="1000"></item>
<item android:drawable="@drawable/b"
android:duration="1000"></item>
<item android:drawable="@drawable/c"
android:duration="1000"></item>
</animation-list>
iv= (ImageView) v.findViewById(R.id.imageView);
iv.setBackgroundResource(R.drawable.frameanimation);
animationDrawable=(AnimationDrawable) iv.getBackground();
animationDrawable.start();
在你的抽屉里放一些图片 然后在drawble name.xml中创建一个xml文件 使用此代码:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/a"
android:duration="1000"></item>
<item android:drawable="@drawable/b"
android:duration="1000"></item>
<item android:drawable="@drawable/c"
android:duration="1000"></item>
</animation-list>
iv= (ImageView) v.findViewById(R.id.imageView);
iv.setBackgroundResource(R.drawable.frameanimation);
animationDrawable=(AnimationDrawable) iv.getBackground();
animationDrawable.start();
您是否尝试过将
BitmapDrawable ball
放在开关盒外?当我将其放在开关盒外时,(R.drawable…)的最后一部分是什么代码>然后呢?这应该是变量,但我认为它需要一个明确的引用,不是吗?你有没有尝试过在你的开关外壳外面放置位图可绘制球
?当我把它放在开关外壳外面时,(R.drawable…)的最后一部分是什么代码>然后呢?这应该是变量,但我认为它需要一个明确的引用,不是吗?那我怎么让它随机选取?那我怎么让它随机选取?好吧