android ImageView遇到的问题
来源:互联网 发布:javascript url编码 编辑:程序博客网 时间:2024/05/25 18:11
1.ImageView显示图片的圆形切图
解决方法:
重写ImageView.onDraw(Canvas) ,把Canvas剪切成圆形,再在上面画图片
protected void onDraw(Canvas canvas) {
intradius =this.getWidth() / 2;
Pathpath =new Path();
path.addCircle(radius,radius,radius, Path.Direction.CW);
canvas.clipPath(path);// 裁剪区域,裁剪成圆形
canvas.save();
if (strokeWidth > 0) {
canvas.drawColor(strokeColor);
path.addCircle(radius -strokeWidth,radius -strokeWidth,radius
+strokeWidth, Path.Direction.CW);
canvas.clipPath(path);
canvas.save();
}
super.onDraw(canvas);
}
2.ImageView显示图片的不同部分
ImageView的setScaleType()方法,提供了几种显示图片的不同部分
ScaleType.FIT_XY:
mDrawable.setBounds(0, 0, vwidth, vheight);
mDrawMatrix = null;
ScaleType.MATRIX:
mDrawable.setBounds(0, 0, dwidth,height);
mDrawMatrix = mMatrix;
ScaleType.CENTER:
mDrawable.setBounds(0, 0, dwidth,height);
mDrawMatrix = mMatrix;
mDrawMatrix.setTranslate((int) ((vwidth - dwidth) * 0.5f + 0.5f),
(int) ((vheight - dheight) * 0.5f + 0.5f)); ScaleType.CENTER_CROP :
mDrawMatrix = mMatrix;
float scale;float dx = 0, dy = 0;
if (dwidth * vheight > vwidth * dheight) {
scale = (float) vheight / (float) dheight;
dx = (vwidth - dwidth * scale) * 0.5f;
} else {
scale = (float) vwidth / (float) dwidth;
dy = (vheight - dheight * scale) * 0.5f;
}
mDrawMatrix.setScale(scale, scale);
mDrawMatrix.postTranslate((int) (dx + 0.5f), (int) (dy + 0.5f));
ScaleType.FIT_START/FIT_END/FIT_CENTER:
mTempSrc.set(0, 0, dwidth, dheight);
mTempDst.set(0, 0, vwidth, vheight);
mDrawMatrix = mMatrix;
mDrawMatrix.setRectToRect(mTempSrc, mTempDst, scaleTypeToScaleToFit(mScaleType));
上面的代码是在 configureBounds()中,而configureBounds()是private方法,主要是修改mDrawMatrix,mDrawable
这两个变量在onDraw()方法中
if (mDrawMatrix != null) {
canvas.concat(mDrawMatrix);
}
mDrawable.draw(canvas);
}
mDrawable.draw(canvas);
如果想要下图效果:
scale = (float) vheight / (float) dheight;
matrix.setScale(scale, scale);
怎样才能把代码加进去?
1.自定义ImageView类,把ImageView的代码全部粘出来的,修改configureBounds(),相对来说比较麻烦
2.继承ImageView,在setFrame,setImageXXX中调用一个自定义方法,把代码加进去。
点击打开链接
0 0
- android ImageView遇到的问题
- 记录ImageView.adjustViewBounds属性 遇到的问题
- Android 笔记 ImageView 显示大图遇到的问题 以及 无损显示大图 的解决方案
- Android获取ImageView上的图片,和一个有可能遇到的问题!
- 关于Android Studio初学时遇到的小问题—ImageView
- ImageView和ViewFlipper使用时遇到的两个小问题
- ImageView设置selector遇到的小问题及解决方案记录
- Android疑难杂症之(ImageView播放gif遇到的坑)
- android imageview设置图片的小问题
- Android中ImageView无法居中的问题
- android中ImageView的内存问题
- android中ImageView的内存问题
- Android中ImageView的内存问题
- android中ImageView的内存问题
- android中ImageView的内存问题
- Android遇到的问题
- android遇到的问题
- android遇到的问题
- 求助 关于比较文本文件中找出相同内容
- LeetCode-从vector到map——在序列中寻找复合条件的组合
- J2ME图层中的应用
- 怎样用云台将mobile按钮覆盖。
- 标题最难写了
- android ImageView遇到的问题
- 给初涉科研同学的建议
- 2017先给自己定一个小目标,比方说先挣一个亿
- 树的基本算法(C语言版)
- 程序打包问题
- 备忘
- 文章标题
- 如何设计Windows Mobile功能导航界面
- 怎样在gridview里添加其他的控件?