【Android学习笔记进阶19 之给图片加边框】出门莫恨无人随,书中车马多如簇。这篇文章主要讲述Android学习笔记进阶19 之给图片加边框相关的知识,希望能为你提供帮助。
文章图片
[java] view plain copy
- //设置颜色
- public void setColour(int color){
- co = color;
- }
- //设置边框宽度
- public void setBorderWidth(int width){
- borderwidth = width;
- }
具体实现:
[java] view plain copy
- package xiaosi.imageborder;
- import android.app.Activity;
- import android.graphics.Color;
- import android.os.Bundle;
- public class ImageBorderActivity extends Activity {
- /** Called when the activity is first created. */
- private myImageView image = null;
- private myImageView image1 = null;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- image = (myImageView)findViewById(R.id.iamge);
- image.setColour(Color.YELLOW);
- image.setBorderWidth(10);
- image1 = (myImageView)findViewById(R.id.iamge1);
- image1.setColour(Color.GREEN);
- image1.setBorderWidth(5);
- }
- }
main.xml
[java] view plain copy
- < LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:background="@drawable/playerbackground"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- < xiaosi.imageborder.myImageView
- android:id="@+id/iamge"
- android:layout_width="200px"
- android:layout_height="230px"
- android:layout_alignParentRight="true"
- android:src="https://www.songbingjia.com/android/@drawable/v"
- android:layout_centerInParent="true"
- android:layout_marginRight="3px"
- />
- < xiaosi.imageborder.myImageView
- android:id="@+id/iamge1"
- android:layout_width="200px"
- android:layout_height="230px"
- android:layout_alignParentRight="true"
- android:src="https://www.songbingjia.com/android/@drawable/v"
- android:layout_centerInParent="true"
- android:layout_marginRight="3px"
- />
- < /LinearLayout>
[java] view plain copy
- package xiaosi.imageborder;
- import android.content.Context;
- import android.graphics.Canvas;
- import android.graphics.Paint;
- import android.graphics.Rect;
- import android.util.AttributeSet;
- import android.widget.ImageView;
- public class myImageView extends ImageView {
- private int co;
- private int borderwidth;
- public myImageView(Context context) {
- super(context);
- }
- public myImageView(Context context, AttributeSet attrs,
- int defStyle) {
- super(context, attrs, defStyle);
- }
- public myImageView(Context context, AttributeSet attrs) {
- super(context, attrs);
- }
- //设置颜色
- public void setColour(int color){
- co = color;
- }
- //设置边框宽度
- public void setBorderWidth(int width){
- borderwidth = width;
- }
- @Override
- protected void onDraw(Canvas canvas) {
- super.onDraw(canvas);
- // 画边框
- Rect rec = canvas.getClipBounds();
- rec.bottom--;
- rec.right--;
- Paint paint = new Paint();
- //设置边框颜色
- paint.setColor(co);
- paint.setStyle(Paint.Style.STROKE);
- //设置边框宽度
- paint.setStrokeWidth(borderwidth);
- canvas.drawRect(rec, paint);
- }
- }
源代码下载:点击打开链接
推荐阅读
- 支付宝---手机APP支付
- Android学习笔记进阶20 之得到图片的缩略图
- 安卓给gallery内&quot;控件&quot;挂载事件,滑动后抬起手指时也触发事件(滑动时不应触发)的解决!
- Python 2.x和Python 3.x之间的重要区别和示例
- 40个常见的Python面试问题和答案合集详细介绍
- 如何修复Android Studio中的“Android studio logcat nothing to show”()
- 面向Android开发人员的10个Android Studio提示和技巧
- 50个常见的数据挖掘面试问题和答案合集详细解析
- 对软件开发人员最有用的10个开发工具合集推荐