http://blog.csdn.net/xiangzhihong8/article/details/17137391
http://blog.csdn.net/xiangzhihong8/article/details/17137391
http://blog.csdn.net/xiangzhihong8/article/details/17137391
http://blog.csdn.net/xiangzhihong8/article/details/17137391
http://blog.csdn.net/xiangzhihong8/article/details/17137391
http://blog.csdn.net/xiangzhihong8/article/details/17137391
http://blog.csdn.net/xiangzhihong8/article/details/17137391
http://blog.csdn.net/xiangzhihong8/article/details/17137391http://blog.csdn.net/xiangzhihong8/article/details/17137391
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
|
/**
* 以一个Bitmap为画布,画上一个Bitmap
* @param canvasBitmap 作为画布的Bitmap
* @param drawBitmap 要被绘制的Bitmap
* @param top 从画布的距离顶部的top位置开始
* @param left 从画布的距离左边的left位置开始
*/
private
void
drawbitMap(Bitmap canvasBitmap, Bitmap drawBitmap,
int
top,
int
left)
{
Canvas localCanvas =
new
Canvas(canvasBitmap);
//以canvasBitmap生成画布
localCanvas.drawBitmap(drawBitmap, left, top,
null
);
//在画布上移left和top左标开始绘制drawBitmap
localCanvas.save(Canvas.ALL_SAVE_FLAG);
//保存
localCanvas.restore();
drawBitmap.recycle();
//释放掉drawBitmap,防止内存泄漏
}
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
|
/**
* 把Drawable生成对应的Bitmap
* @param paramRect 生成的Bitmap大小等一些参数
* @param drawable 要绘制的drawable
* @param canvasBitmap 将drawable绘制到canvasBitmap中
*/
private
void
getBitMap(Rect paramRect, Drawable drawable, Bitmap canvasBitmap)
{
//中这个方法顾名思义,就是设置边界,
//用到的是.9图,所以拉伸图片不会失真,把drawable设置一个left、top点开始拉一个paramRect.right宽
//paramRect.bottom高的矩形区域,按我的理解就是弄了这个区域,就是把图片按.9图的设置拉倒相应的
//大小填充到矩形区域里去,不明白看 的博客
drawable.setBounds(
0
,
0
, paramRect.right, paramRect.bottom);
//用canvasBitmap生成一个画布
Canvas localCanvas =
new
Canvas(canvasBitmap);
drawable.draw(localCanvas);
//使用drawable的draw方法画到画布上
localCanvas.save(Canvas.ALL_SAVE_FLAG);
//保存
localCanvas.restore();
}
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
|
//定义3个图片的大小等一些参数
Rect[] arrayOfRect =
new
Rect[
3
];
arrayOfRect[
0
] =
new
Rect();
arrayOfRect[
0
].top =
0
;
arrayOfRect[
0
].left =
0
;
arrayOfRect[
0
].right =
this
.parentLeft +
this
.parentWidth /
4
;
arrayOfRect[
0
].bottom =
this
.popupWindowHeight;
arrayOfRect[
1
] =
new
Rect();
arrayOfRect[
1
].top =
0
;
arrayOfRect[
1
].left = arrayOfRect[
0
].right;
arrayOfRect[
1
].right =
this
.parentWidth /
2
;
arrayOfRect[
1
].bottom =
this
.popupWindowHeight;
arrayOfRect[
2
] =
new
Rect();
arrayOfRect[
2
].top =
0
;
arrayOfRect[
2
].left =
this
.parentLeft +
this
.parentWidth *
3
/
4
;
arrayOfRect[
2
].right = screenwidth - arrayOfRect[
2
].left;
arrayOfRect[
2
].bottom =
this
.popupWindowHeight;
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
/**
* 生成背景图
* @param context 上下文,为生成BitmapDrawable所需的
* @param ArrayOfRect
* @param ArrayOfDrawable
* @return
*/
private
Drawable getDrawable(Context context,
Rect[] ArrayOfRect, Drawable[] ArrayOfDrawable)
{
Bitmap.Config localConfig = Bitmap.Config.ARGB_8888;
//先更具popupWindow的大小生成一个Bitmap
Bitmap paramBitmap = Bitmap.createBitmap(screenwidth, popupWindowHeight, localConfig);
//这里循环把3个图片绘制到paramBitmap中
for
(
int
i =
0
; i < ArrayOfDrawable.length; i++)
{
Rect localRect = ArrayOfRect<i>;
Bitmap localBitmap = Bitmap.createBitmap(localRect.right, localRect.bottom, localConfig);
Drawable localDrawable = ArrayOfDrawable</i><i><i>;
getBitMap(localRect, localDrawable, localBitmap);
//得到相应的drawable的BitMap
drawbitMap(paramBitmap, localBitmap, localRect.top, localRect.left);
//在paramBitmap中绘制localBitmap
localBitmap.recycle();
//释放掉,要不多次运行有可能会内存泄漏
}
return
new
BitmapDrawable(context.getResources(), paramBitmap);
}</i></i>
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
|
public
void
show(View parent, Context context) {
bg = mapDrawable.get(parent);
//为节省资源,map中会保存以前生成的背景,根据父控件来获得
popupWindowHeight = popupWindow.getHeight();
//得到popupWindow的高度,在popupWindow构造完后才能获取
this
.parentLeft = parent.getLeft();
//父控件的左边距
this
.parentWidth = parent.getWidth();
//父控件的宽度
if
(bg ==
null
)
//背景为空
{
createDrawable(context);
//生成背景图
mapDrawable.put(parent, bg);
//保存到map中
}
popupWindow.setBackgroundDrawable(bg);
//给popupWindow设置背景
popupWindow.showAtLocation(parent, Gravity.BOTTOM,
0
, parent.getHeight());
// 距离底部的位置
popupWindow.setAnimationStyle(R.style.popwin_anim_style);
popupWindow.setFocusable(
true
);
popupWindow.setOutsideTouchable(
true
);
popupWindow.update();
}
|