The processing method of bookshelf effect background image of Android programming _android

Source: Internet
Author: User

This article is an example of the processing method of bookshelf effect background image of Android programming. Share to everyone for your reference, specific as follows:

In Android applications, doing a novel reader is the idea of many people, we are generally how to read large files, if the scrolling or page, so that users do not feel the focus. I'm also doing a similar function, but in the bookshelf, read the QQ bookshelf, feel very good-looking, want to do a, the previous "Android Bookshelf effect Principle and code" on this made a special introduction, its complete example code can click here to download the site.

The above example is very good, but there is a problem is that his background image width must be the width of the phone screen, will not change, so that the phone is not good for the fitness. I made a change on my own hands.

Don't say more, just say the principle code.

There is no other way to add a background to the GridView, can only rewrite the GridView class, in this class, you load a background map to deal with, my practice is after loading the background image, the phone based on the width of your mobile to draw a new map, so that your background map, you can look good display.

Here I only release the rewritten GridView class.

public class MyGridView extends GridView {private Bitmap background;
 Private Bitmap Newbackground;
  Public MyGridView (context, AttributeSet attrs) {Super (context, attrs);
 Load as background picture background = Bitmapfactory.decoderesource (Getresources (), R.DRAWABLE.BOOKCASE_BG);
  } protected void Dispatchdraw (Canvas Canvas) {//Get loaded background picture high, wide int width = background.getwidth ();
  int height = background.getheight ();
  Get the phone screen high, wide, here to note, do not write in the construction method, because the value is 0 int scwidth = this.getwidth ();
  int scheight = This.getheight ();
  Calculate the zoom rate, the new size in addition to the original size, I am here because the high does not change, so is the original size ratio 1//Here must note that the value here is proportional, not value.
  float ScaleWidth = ((float) scwidth)/width;
  float ScaleHeight = 1;  LOG.V ("Info", "width:" + width + "Height:" + height + "Scwidth:" + scwidth + "ScaleWidth:" + ScaleWidth + "ScaleHeight:"
  + ScaleHeight);
  Matrix matrix for creating an action picture matrix = new Matrix ();
  Zoom picture Action Matrix.postscale (ScaleWidth, ScaleHeight); Generate a new picture Newbackground = Bitmap.createbitmap (background, 0, 0, WIdth, height, matrix, true);
  int count = Getchildcount ();
  LOG.V ("Mygridview-count", Count + "");
  int top = 185;
  LOG.V ("Getchildat", Getchildat (0). GetTop () + "");
  int backgroundwidth = Newbackground.getwidth ();
  int backgroundheight = Newbackground.getheight (); for (int y = top; y<scheight; y = = 223) {//for (int x = 0; x<scwidth; x + = backgroundwidth) {Canvas.drawbitma
   P (newbackground, 0, y, null);
 } super.dispatchdraw (canvas); //Disable scroll bar scrolling public void onmeasure (int widthmeasurespec, int heightmeasurespec) {int expandspec = Measurespec.makeme
   Asurespec (Integer.max_value >> 2, measurespec.at_most);
  Super.onmeasure (Widthmeasurespec, Expandspec);

 }
}

I hope this article will help you with your Android programming.

Related Article

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.