我有一个带有width = 1080
和height = 1920
我有width = 3340
和height = 1920
我将imageView的缩放类型设置为MATRIX。这样位图就不会缩放到imageView
imageView.setScaleType(ImageView.ScaleType.MATRIX);
我想从左到右为位图设置动画,反之亦然。
有可能吗?有什么想法吗?
答案 0 :(得分:2)
请在项目中创建以下自定义视图类
package com.company.xyz;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
/**
* Created by rohitp on 12/10/2015.
*/
public class CustomImageView extends View {
Bitmap bm;
int x, y;
boolean flag = true;
public CustomImageView(Context context) {
super(context);
}
public CustomImageView(Context context, AttributeSet attrs) {
super(context, attrs);
try{
int src_resource = attrs.getAttributeResourceValue("http://schemas.android.com/apk/res/android", "src", 0);
bm = getDrawable(getResources(),src_resource);
}catch (Exception e){
}
}
public static Bitmap getDrawable(Resources res, int id){
return BitmapFactory.decodeResource(res, id);
}
public void setBm(Bitmap bm) {
this.bm = bm;
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
if(bm != null){
if(flag){
if (x < (getMeasuredWidth()-bm.getWidth())) {
x += 10;
Log.e("Custom",x+""+getMeasuredWidth());
}
else {
Log.e("Custom",x+" false");
flag = false;
}
}else{
if (x >0 ) {
x -= 10;
Log.e("Custom",x+" -");
}
else {
flag = true;
Log.e("Custom",x+" true");
}
}
canvas.drawBitmap(bm, x, y, new Paint());
}
invalidate();//calls this method again and again
}
}
使用以下
创建布局xml<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<com.company.xyz.CustomImageView
android:layout_width="match_parent"
android:layout_height="100dp"
android:id="@+id/animate_pic"
android:src="@drawable/test">
</com.company.xyz.CustomImageView>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button"
android:layout_gravity="bottom"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
您还可以在运行时通过代码更改位图
((CustomImageView) findViewById(R.id.animate_pic)).setBm(bm)
答案 1 :(得分:0)
根据此链接http://www.sanfoundry.com/java-android-program-animante-bitmap/中的示例,您需要使用onDraw()
内的画布移动图像。所以,请点击此链接,这可能会对您有所帮助。如有任何问题,请告诉我。