Android Background image Scaling implementation _android

Source: Internet
Author: User

Zoom for Android background image

One Goal, one passion!

As we see some effects, the background picture in the control will grow larger, but the control will not grow larger as the picture is magnified. The effect is as follows:

Analysis:

To make the picture larger and the size of the control itself unchanged, change the size of the picture itself, not the size of the control.

Implementation principle:

1, first get the picture we want to enlarge bitmap.

2, use Bitmap.createbitmap (). Create a copy of the bitmap.

3, use the matrix to change the size of the picture copy itself

4, use Valueanimator to draw the copy according to the rate of change.

customizing view

 public class Scaleimage extends View {/** * Set background picture * * Private drawable background;
  /** * Canvas background picture * * Private Bitmap bitmapcopy;

  /** * Follow the animation real-time update magnification/float scal = 1f;
  /** * To enlarge the original image 1.3 times times, this value can be changed at will. The purpose is to fill the original image with control/private float Orgfrac = 1.3f;
  /** * Control wide */private int widthsize;
  /** * Control High * * private int heightsize;
  private float DownY;


  private float Downx;
  Public Scaleimage {This (context, NULL);
  Public Scaleimage (context, AttributeSet attrs) {This (context, attrs, 0);
  Public Scaleimage (context, AttributeSet attrs, int defstyleattr) {Super (context, attrs, defstyleattr); @Override protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {super.onmeasure (widthmeasure


    Spec, 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); 
       /** * Float SX, float sy, float px, float py * * Sx,sy x,y Direction Scaling * px,py scaling with px py * The magnification ratio is added with the default Orgfrac. It is designed to fill the control when the zoom is not yet started. If the picture is too small, there may be a boundary gap between the control and the picture * Note: here's px
       Py:matrix is acting on which object, then px,py is the coordinate point on the object * such as: here is the px,py coordinate point on the bitmapcopy.



      * * Matrix.setscale (Orgfrac + scal, 1, Bitmapcopy.getwidth ()/2, Bitmapcopy.getheight ()/2);

    Canvas.drawbitmap (bitmapcopy, matrix, NULL); }/** * Start scaling * * @param drawableid need to enlarge the background picture */public void Startscale (int drawableid) {back Ground = 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 Onanimationu

          Pdate (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 Motion
        Event.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; /** * Click event Monitor * * @param listener/public void Addbackgroundcilcklistener (Onbackgroundcilcklistener listen
  ER) {this.listener = listener;
  public interface Onbackgroundcilcklistener {void Backgroundclick ();

 }


  }

Run up

 Image = (scaleimage) Findviewbyid (r.id.image);

    Image.startscale (r.drawable.parallax_img);

    Image.addbackgroundcilcklistener (New Scaleimage.onbackgroundcilcklistener () {
      @Override public
      Void Backgroundclick () {

      }
    });

Violin Home

Matrix use to Be continued

All right. Directly using the control, we'll pass the drawable in the resource file.

Thank you for reading, I hope to help you, thank you for your support for this site!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.