1. 程式人生 > >Android 使用Canvas中的drawBitmap方法繪製拉伸的圖片

Android 使用Canvas中的drawBitmap方法繪製拉伸的圖片

package com.vision.agriculture.classes.repository;


import android.content.Context;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.graphics.Canvas;

import android.graphics.Rect;

import android.graphics.RectF;

import android.util.AttributeSet;

import android.widget.GridView
;


import com.vision.agriculture.R;


publicclassMyGridViewextendsGridView{
privateBitmap background;
publicMyGridView(Context context,AttributeSet attrs){
super(context, attrs);
          background =BitmapFactory.decodeResource(getResources(),
                    R.drawable.repo_gridview_layer_bg);
}

@Override

protectedvoid layoutChildren(){
super.layoutChildren();
}

@Override
protectedvoid onMeasure(int widthMeasureSpec,intheightMeasureSpec){
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}


@Override
protectedvoid dispatchDraw(Canvas canvas){
int count = getChildCount();
int top = count >0? getChildAt(0).getTop
():0;
int backgroundWidth = background.getWidth();
int backgroundHeight = background.getHeight();
int width = getWidth();
int height = getHeight();
finalRect src =newRect();// 影象內的座標
          src.left =0;
          src.top =0;
          src.right = backgroundWidth;
          src.bottom = backgroundHeight;
for(int y = top; y < height; y += backgroundHeight){
RectF dst =newRectF();//螢幕
              dst.left =0;
              dst.top = y;
              dst.right = width;
              dst.bottom = y+backgroundHeight;
              canvas.drawBitmap(background, src, dst,null);
}
super.dispatchDraw(canvas);
}
}