Example: Background without stretching

Quickly share your Android Code Snippets here...

Example: Background without stretching

Postby kostmo » Thu Sep 24, 2009 4:31 am

This has been asked a couple of times on the Google Groups.

The following class wraps a Drawable resource to use as a background for a View without stretching. Adjust the padding as desired.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class NonScalingBackgroundDrawable extends Drawable {
  2.  
  3.  
  4.  
  5.         int padding_top = 30;
  6.  
  7.         int padding_bottom = 10;
  8.  
  9.  
  10.  
  11.         int padding_left = 20;
  12.  
  13.         int padding_right = 10;
  14.  
  15.        
  16.  
  17.         Context context;
  18.  
  19.         View view;
  20.  
  21.         Drawable hosted_drawable;
  22.  
  23.         public NonScalingBackgroundDrawable(Context c, View v, int resource) {
  24.  
  25.                 context = c;
  26.  
  27.                 view = v;
  28.  
  29.                 hosted_drawable = context.getResources().getDrawable(resource);
  30.  
  31.         }
  32.  
  33.        
  34.  
  35.         public void draw(Canvas canvas) {
  36.  
  37.  
  38.  
  39.                 int w = hosted_drawable.getIntrinsicWidth();
  40.  
  41.                 int h = hosted_drawable.getIntrinsicHeight();
  42.  
  43.  
  44.  
  45.                 int view_w = view.getWidth();
  46.  
  47.                 int view_h = view.getHeight();
  48.  
  49.  
  50.  
  51.                 int padded_horizontal_room = view_w - (padding_left + padding_right);
  52.  
  53.                 int padded_vertical_room = view_h - (padding_top + padding_bottom);
  54.  
  55.                
  56.  
  57.                 float scale;
  58.  
  59.                 float intrinsic_aspect_ratio = w / (float) h;
  60.  
  61.                 float padded_canvas_aspect_ratio = padded_horizontal_room / (float) padded_vertical_room;
  62.  
  63.                 if (intrinsic_aspect_ratio > padded_canvas_aspect_ratio)
  64.  
  65.                         // Our source image is wider than the canvas, so we scale by width.
  66.  
  67.                         scale = padded_horizontal_room / (float) w;
  68.  
  69.                 else
  70.  
  71.                         scale = padded_vertical_room / (float) h;
  72.  
  73.                
  74.  
  75.                 int scaled_width = (int) (scale*w);
  76.  
  77.                 int scaled_height = (int) (scale*h);
  78.  
  79.  
  80.  
  81.                 // Here we fit the image into the bottom-right corner.
  82.  
  83.                 int left = view_w - scaled_width - padding_right;
  84.  
  85.                 int top = view_h - scaled_height - padding_bottom;
  86.  
  87.                 int right = view_w - padding_right;
  88.  
  89.                 int bottom = view_h - padding_bottom;
  90.  
  91.  
  92.  
  93.                 hosted_drawable.setBounds(
  94.  
  95.                         left,
  96.  
  97.                         top,
  98.  
  99.                         right,
  100.  
  101.                         bottom
  102.  
  103.                 );
  104.  
  105.                
  106.  
  107.                 hosted_drawable.draw(canvas);
  108.  
  109.         }
  110.  
  111.  
  112.  
  113.         public int getOpacity() {
  114.  
  115.                 return hosted_drawable.getOpacity();
  116.  
  117.         }
  118.  
  119.  
  120.  
  121.         public void setAlpha(int alpha) {
  122.  
  123.                 hosted_drawable.setAlpha(alpha);
  124.  
  125.         }
  126.  
  127.  
  128.  
  129.         public void setColorFilter(ColorFilter cf) {
  130.  
  131.                 hosted_drawable.setColorFilter(cf);
  132.  
  133.         }
  134.  
  135. }
Parsed in 0.036 seconds, using GeSHi 1.0.8.4
kostmo
Freshman
Freshman
 
Posts: 5
Joined: Thu Sep 24, 2009 4:23 am

Top

Postby elachys » Wed Apr 07, 2010 4:40 pm

How would you center the image instead of placing it in the bottom right?
elachys
Freshman
Freshman
 
Posts: 3
Joined: Wed Apr 07, 2010 4:33 pm

Re: Example: Background without stretching

Postby zuerst » Wed Nov 02, 2011 10:16 am

body
{
backg round-image:url('smiley.gif');
background-repeat:no-repeat;
background-attachment:fixed;
background-position:center;
}
zuerst
Freshman
Freshman
 
Posts: 4
Joined: Wed Nov 02, 2011 8:06 am

Top

Return to Code Snippets for Android

Who is online

Users browsing this forum: Yahoo [Bot] and 5 guests