栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

Android 背景图片的缩放实现

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android 背景图片的缩放实现

Android 背景图片的缩放

 ONE Goal ,ONE Passion !

我们看到一些效果,控件中的背景图片会慢慢变大,但是控件不会随着图片的放大而变大.效果如下:

分析:

想让图片变大,而且控件本身大小不能改变,那么就要改变图片自身大小,而不能改变控件大小.

实现原理:

1,首先拿到我们要放大的图片bitmap.

2,使用Bitmap.createBitmap().创建一个bitmap的副本.

3,使用matrix去改变图片副本本身大小

4,使用ValueAnimator去根据变化率将副本绘制出来.

自定义View

 public class ScaleImage extends View {

  
  private Drawable background;
  
  private Bitmap bitmapCopy;
  
  float scal = 1f;

  
  private float orgFrac = 1.3f;
  
  private int widthSize;
  
  private int heightSize;
  private float downY;
  private float downX;


  public ScaleImage(Context context) {
    this(context, null);
  }

  public ScaleImage(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
  }

  public ScaleImage(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
  }

  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);


    widthSize = MeasureSpec.getSize(widthMeasureSpec);
    heightSize = MeasureSpec.getSize(heightMeasureSpec);


  }

  @Override
  protected void onDraw(final Canvas canvas) {
    super.onDraw(canvas);


    if (background != null) {
      BitmapDrawable bd = (BitmapDrawable) background;
      final Bitmap bitmap = bd.getBitmap();


      final Matrix matrix = new Matrix();

      bitmapCopy = Bitmap.createBitmap(bitmap, 0, 0,
   bitmap.getWidth(), bitmap.getHeight(),
   matrix, true);


      

      matrix.setScale(orgFrac + scal, 1, bitmapCopy.getWidth() / 2, bitmapCopy.getHeight() / 2);



      canvas.drawBitmap(bitmapCopy, matrix, null);

    }


  }

  
  public void startScale(int drawableId) {


    background = getResources().getDrawable(drawableId);
    if (background == null) {
      throw new RuntimeException("background must not null");
    } else {


      ValueAnimator animator = ValueAnimator.ofFloat(0, 1);
      animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
 @Override
 public void onAnimationUpdate(ValueAnimator animation) {

   float fraction = (float) animation.getAnimatedValue();

   scal = (float) (0.5 * fraction);
   invalidate();


 }
      });

      animator.setDuration(5000);
      animator.setInterpolator(new BounceInterpolator());

      animator.start();

    }

  }

  @Override
  public boolean onTouchEvent(MotionEvent event) {

    switch (event.getAction()) {
      case MotionEvent.ACTION_DOWN:

 downY = event.getY();
 downX = event.getX();

 break;
      case MotionEvent.ACTION_UP:

 float upY = event.getY();
 float upX = event.getX();


 if (Math.abs(upY - downY) < 5 && Math.abs(upX - downX) < 5) {
   listener.backgroundClick();
 }

 break;
    }


    return true;
  }

  onBackgroundCilckListener listener;

  
  public void addBackgroundCilckListener(onBackgroundCilckListener listener) {
    this.listener = listener;
  }


  public interface onBackgroundCilckListener {
    void backgroundClick();
  }


  }

跑起来

 image = (ScaleImage) findViewById(R.id.image);

    image.startScale(R.drawable.parallax_img);

    image.addBackgroundCilckListener(new ScaleImage.onBackgroundCilckListener() {
      @Override
      public void backgroundClick() {

      }
    });

小提琴家

matrix使用待续

好了.直接使用控件,我们将资源文件中的Drawable传入就可以了.

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/160740.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号