精华内容
下载资源
问答
  • android图片圆形裁剪框

    2018-02-23 13:52:11
    手机拍照后、选择照片后的圆形裁剪框 demo,简单实用!!
  • Android图片圆形和矩形截图功能

    千次阅读 2018-04-14 11:21:01
    圆形矩形头像裁剪框

    项目中经常会用到一些图片的裁剪作为头像,需求基本是圆形或者是矩形的。
    1、先上个效果图
    矩形裁剪框
    圆形裁剪框

    2、背景是由选择图片传递过去显示的

    主要思路
    1、矩形在四周绘制四个矩形,为透明的黑色,注意绘制的四个矩形不要重叠,不然会有黑色的重叠框子无法查看,绘制完成边框之后,在中间绘制一个有白色边框的矩形。
    2、圆形在四周绘制一个半径为高度一半的圆弧,为透明的黑色,绘制完边框之后,在中间绘制一个有白色变宽的圆形
    3、获取bitmap,使用Bitmap.drawBitmap();方法,获取矩形或者圆形的bitmap

    3、矩形主要代码

    @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            int width = this.getWidth();
            int height = this.getHeight();
    
            mWidth = width - mHorizontalPadding * 2;
            mVerticalPadding = (height - mWidth) / 2;
            mPaint.setColor(0xaa000000);
            //绘制左边
            canvas.drawRect(0, 0, mHorizontalPadding, height, mPaint);
            //绘制上边
            canvas.drawRect(mHorizontalPadding, 0, width - mHorizontalPadding, mVerticalPadding, mPaint);
            //绘制底部
            canvas.drawRect(mHorizontalPadding, height - mVerticalPadding, width - mHorizontalPadding, height, mPaint);
            //绘制右边
            canvas.drawRect(width - mHorizontalPadding, 0, width, height, mPaint);
            //绘制矩形边框
            mPaint.setColor(mBorderColor);
            mPaint.setStrokeWidth(mBorderWidth);
            mPaint.setStyle(Paint.Style.STROKE);
            canvas.drawRect(mHorizontalPadding, mVerticalPadding, width - mHorizontalPadding, height - mVerticalPadding, mPaint);
        }

    4、圆形主要代码

    @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            int width = this.getWidth();
            int height = this.getHeight();
    
            int innerCircle = dip2px(mContext, 150); 
            int ringWidth = height;
    
            //绘制圆形
            mPaint.setColor(Color.WHITE);
            mPaint.setStrokeWidth(2);
            mPaint.setStyle(Paint.Style.STROKE);
            canvas.drawCircle(width / 2, height / 2, innerCircle, mPaint);
    
            //绘制圆形周边的背景
            mPaint.setColor(0xaa000000);
            mPaint.setStrokeWidth(ringWidth);
            canvas.drawCircle(width / 2, height / 2, innerCircle + 1 + ringWidth
                    / 2, mPaint);
    
        }

    5、布局主要代码

    <com.tongju.common.widget.view.ClipImageView
            android:id="@+id/src_pic"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    
        <com.tongju.common.widget.view.ClipRectangleView
            android:id="@+id/clipview"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />

    6、Activity主要代码

    @Route(path = RouterPath.ROUTER_COMMON_IMAGE_CLIP)
    public class ClipImageActivity extends BaseActivity implements View.OnClickListener {
    
        private ClipImageView mClipImageView;
    
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_clip_image);
        }
    
        @Override
        protected BasePresenter createPresenter() {
            return null;
        }
    
        @Override
        protected void findViews() {
            this.mClipImageView = findViewById(R.id.src_pic);
            findViewById(R.id.iv_back).setOnClickListener(this);
            findViewById(R.id.tv_use).setOnClickListener(this);
        }
    
        @Override
        protected void init() {
        //拍照或者选完相册照片传递uri过来
            Uri uri = getIntent().getParcelableExtra("image");
            Glide.with(this).load(uri).apply(new RequestOptions()
                    .skipMemoryCache(true)
                    .diskCacheStrategy(DiskCacheStrategy.NONE))
                    .transition(withCrossFade()).into(mClipImageView);
        }
    
        @Override
        public void onClick(View v) {
            int id = v.getId();
            if (id == R.id.iv_back) {
                finish();
            } else if (id == R.id.tv_use) {
                Bitmap bitmap = mClipImageView.rectangleClip();
                //这边通过EventBus传递获取到的bitmap,因为bundle对bitmap传递有大小限制
                EventBus.getDefault().post(new MGetBitmap(bitmap));
                finish();
            }
        }
    
    }
    

    github代码
    https://github.com/llayjun/android_common_use/tree/master/%E7%9F%A9%E5%BD%A2%E5%9C%86%E5%BD%A2%E8%A3%81%E5%89%AA%E5%A4%B4%E5%83%8F%E5%8A%9F%E8%83%BD

    展开全文
  • Android自定义圆形图片

    千次下载 热门讨论 2013-12-17 17:09:31
    Android自定义圆形图片,可设置最多两个的外边框。包括从网络获取图片显示。 解决图片锯齿问题。 解决图片变形问题。 文章地址:http://blog.csdn.net/alan_biao/article/details/17379925
  • Android圆形图片

    2018-09-25 19:41:32
    Android圆形图片处理工具类jar包,导入项目可直接使用
  • 主要为大家详细介绍了Android实现圆形图片或者圆角图片的代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
  • Android自定义圆形图片和圆角图片
  • Android裁剪图片圆形图片Demo Android裁剪图片圆形图片Demo Android裁剪图片圆形图片Demo Android裁剪图片圆形图片Demo
  • Android自定义圆形图片,可设置最多两个的外边框。包括从网络获取图片显示。 解决图片锯齿问题。 解决图片变形问题 有需要的可以下载,效果图在 http://blog.csdn.net/wangqing830414/article/details/39612421
  • Android 圆形图片 CircleImageView
  • Android自定义圆形圆角图片示例
  • Android 圆形图片代码

    2017-04-05 12:03:23
    Android处理圆形图片的代码。
  • android自定义圆形头像图片,带底部半透明text文本,可设置颜色 透明度 字体大小。
  • android圆形图片

    2016-06-29 14:42:42
    android开源系列:CircleImageView自定义圆形控件的使用
  • Android实现圆形图片

    2019-09-24 03:01:50
    圆形图片现在越来越多应用于Android应用中, 但是用的最多的地方当属于, 头像的显示. 比如QQ, 新浪微博,微信中很多地方都是用了圆形头像,所以有必要深究一下. 二.圆形图片的实现方式 1.使用自定义控件 2.使用第三...

    一.概述

    圆形图片现在越来越多应用于Android应用中, 但是用的最多的地方当属于, 头像的显示. 比如QQ, 新浪微博,微信中很多地方都是用了圆形头像,所以有必要深究一下.

    二.圆形图片的实现方式

    1.使用自定义控件

    2.使用第三方库

    下面先说第1种, 自定义控件

     

    package com.example.kun.widget;
    
    import android.content.Context;
    import android.content.res.TypedArray;
    import android.graphics.Bitmap;
    import android.graphics.Bitmap.Config;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.graphics.Path;
    import android.graphics.PorterDuff;
    import android.graphics.PorterDuffXfermode;
    import android.graphics.RectF;
    import android.util.AttributeSet;
    import android.widget.ImageView;
    
    import com.example.kun.R;
    
    /**
     * 自定义圆形图片
     */
    public class RoundAngleImageView extends ImageView {
        //定义2个画笔
        private Paint paint;
        private Paint paint2;
        private int roundWidth = 5;
        private int roundHeight = 5;
        //以下是必须实现的构造方法
        public RoundAngleImageView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            init(context, attrs);
        }
    
        public RoundAngleImageView(Context context, AttributeSet attrs) {
            super(context, attrs);
            init(context, attrs);
        }
    
        public RoundAngleImageView(Context context) {
            super(context);
            init(context, null);
        }
    
        private void init(Context context, AttributeSet attrs) {
            if (attrs != null) {
                TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RoundAngleImageView);
                //根据像素赋值
                roundWidth = a.getDimensionPixelSize(R.styleable.RoundAngleImageView_roundWidth, roundWidth);
                roundHeight = a.getDimensionPixelSize(R.styleable.RoundAngleImageView_roundHeight, roundHeight);
            } else {
                //根据设备密度计算
                float density = context.getResources().getDisplayMetrics().density;
                roundWidth = (int) (roundWidth * density);
                roundHeight = (int) (roundHeight * density);
            }
    
            paint = new Paint();
            paint.setColor(Color.WHITE);
            paint.setAntiAlias(true);//设置图片抗锯齿,就是设置图片边缘锯齿感 不明显
            //设置2张图片相交的模式, 圆形覆盖 方形
            paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
    
            paint2 = new Paint();
            paint2.setXfermode(null);
        }
    
        @Override
        public void draw(Canvas canvas) {
            Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Config.ARGB_8888);
            Canvas canvas2 = new Canvas(bitmap);
            super.draw(canvas2);
            drawLiftUp(canvas2);
            drawRightUp(canvas2);
            drawLiftDown(canvas2);
            drawRightDown(canvas2);
            canvas.drawBitmap(bitmap, 0, 0, paint2);
            bitmap.recycle();
        }
    
        private void drawLiftUp(Canvas canvas) {
            Path path = new Path();
            path.moveTo(0, roundHeight);
            path.lineTo(0, 0);
            path.lineTo(roundWidth, 0);
            path.arcTo(new RectF(
                            0,
                            0,
                            roundWidth * 2,
                            roundHeight * 2),
                    -90,
                    -90);
            path.close();
            canvas.drawPath(path, paint);
        }
    
        private void drawLiftDown(Canvas canvas) {
            Path path = new Path();
            path.moveTo(0, getHeight() - roundHeight);
            path.lineTo(0, getHeight());
            path.lineTo(roundWidth, getHeight());
            path.arcTo(new RectF(
                            0,
                            getHeight() - roundHeight * 2,
                            0 + roundWidth * 2,
                            getHeight()),
                    90,
                    90);
            path.close();
            canvas.drawPath(path, paint);
        }
    
        private void drawRightDown(Canvas canvas) {
            Path path = new Path();
            path.moveTo(getWidth() - roundWidth, getHeight());
            path.lineTo(getWidth(), getHeight());
            path.lineTo(getWidth(), getHeight() - roundHeight);
            path.arcTo(new RectF(
                    getWidth() - roundWidth * 2,
                    getHeight() - roundHeight * 2,
                    getWidth(),
                    getHeight()), 0, 90);
            path.close();
            canvas.drawPath(path, paint);
        }
    
        private void drawRightUp(Canvas canvas) {
            Path path = new Path();
            path.moveTo(getWidth(), roundHeight);
            path.lineTo(getWidth(), 0);
            path.lineTo(getWidth() - roundWidth, 0);
            path.arcTo(new RectF(
                            getWidth() - roundWidth * 2,
                            0,
                            getWidth(),
                            0 + roundHeight * 2),
                    -90,
                    90);
            path.close();
            canvas.drawPath(path, paint);
        }
    }

    对应的attrs.xml

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <declare-styleable name="RoundAngleImageView">
            <attr name="roundWidth" format="dimension" />
            <attr name="roundHeight" format="dimension" />
        </declare-styleable>
    </resources>

    布局文件:

    <com.example.kun.widget.RoundAngleImageView
            android:id="@+id/photoView"
            android:src="@mipmap/temp"
            app:roundHeight="100dp"
            app:roundWidth="100dp"
            android:scaleType="fitXY"
            android:layout_width="200dp"
            android:layout_height="200dp" />

    布局需要注意: 自定义属性必须要有,并且宽高必须为 layout_width和layout_height的一半

    还有一点注意:

    android:scaleType="fitXY"这个属性必须要有,否则不会实现图片圆形

    java代码如下:

    public class MainActivity extends AppCompatActivity {
        private RoundAngleImageView photoView;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            initialize();
        }
    
        private void initialize() {
            photoView = (RoundAngleImageView) findViewById(R.id.photoView);
        }
    }

    运行效果图:

     

    以上的自定义控件多少还是有些麻烦的 ,下面搞一个稍微简单点的工具类

    public class CircleTransform extends BitmapTransformation {
        public CircleTransform(Context context) {
            super(context);
        }
    
        @Override
        protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
            return circleCrop(pool, toTransform);
        }
    
        private static Bitmap circleCrop(BitmapPool pool, Bitmap source) {
            if (source == null) return null;
    
            int size = Math.min(source.getWidth(), source.getHeight());
            int x = (source.getWidth() - size) / 2;
            int y = (source.getHeight() - size) / 2;
    
            Bitmap squared = Bitmap.createBitmap(source, x, y, size, size);
    
            Bitmap result = pool.get(size, size, Bitmap.Config.ARGB_8888);
            if (result == null) {
                result = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
            }
    
            Canvas canvas = new Canvas(result);
            Paint paint = new Paint();
            paint.setShader(new BitmapShader(squared, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
            paint.setAntiAlias(true);
            float r = size / 2f;
            canvas.drawCircle(r, r, r, paint);
            return result;
        }
    
        @Override
        public String getId() {
            return getClass().getName();
        }
    }

    布局如下:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:gravity="center"
        android:layout_height="match_parent">
    
        <ImageView
            android:scaleType="fitXY"
            android:id="@+id/photoView"
            android:layout_width="100dp"
            android:layout_height="100dp" />
    </LinearLayout>

    java代码:

    public class MainActivity extends AppCompatActivity {
        private ImageView photoView;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            initialize();
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Glide.with(MainActivity.this).load(Uri.parse("http://pic13.nipic.com/20110415/1347158_132411659346_2.jpg"))
                            .transform(new CircleTransform(MainActivity.this))
                            .placeholder(R.mipmap.ic_launcher)
                            .error(R.mipmap.ic_launcher)
                            .into(photoView);
                }
            });
        }
        private void initialize() {
            photoView = (ImageView) findViewById(R.id.photoView);
        }
    }

    以上代码使用GLide加载图片,需要引入glide库

    运行截图:

     

    分割线---------------------------------------------------------------------------------------------------------------------

    以上方式虽然能实现图片圆形化,但是都是比较麻烦的,下面介绍一种我最常用的,也是最简单的一个方法

    CircleImageView 

    github:https://github.com/hdodenhof/CircleImageView

    直接上代码

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:gravity="center"
        android:layout_height="match_parent">
    
        <!--加入了边框宽度和颜色-->
        <de.hdodenhof.circleimageview.CircleImageView
            app:civ_border_width="2dp"
            app:civ_border_color="#FFFFFF"
            android:id="@+id/photoView"
            android:src="@mipmap/ic_launcher"
            android:layout_width="100dp"
            android:layout_height="100dp" />
    </LinearLayout>
    public class MainActivity extends AppCompatActivity {
        private CircleImageView photoView;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            initialize();
            Picasso.with(this).load(R.mipmap.temp).into(photoView);
        }
    
        private void initialize() {
            photoView = (CircleImageView) findViewById(R.id.photoView);
        }
    }

    引入的库文件

    compile 'de.hdodenhof:circleimageview:2.0.0'
    compile 'com.squareup.picasso:picasso:2.5.2'
    运行截图:最外层带有一个白色边框

    
    

    转载于:https://www.cnblogs.com/android-zcq/p/5138462.html

    展开全文
  • Android 圆形图片

    2014-09-29 01:58:21
    Android 圆形图片
  • android圆形图片控件

    2019-01-11 16:07:10
    android圆形图片控件,在项目开发中,我们经常需要用到圆形图片效果,典型案例是用户头像的显示。
  • Android 绘制圆形图片

    2017-06-06 10:56:46
    Android 绘制圆形图片 MainActivity  package com.example.roundbitmap_demo; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLExce

    Android 绘制圆形图片

    MainActivity 

    package com.example.roundbitmap_demo;

    import java.io.IOException;
    import java.io.InputStream;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.net.URLConnection;
    import android.app.Activity;
    import android.graphics.Bitmap;
    import android.graphics.Bitmap.Config;
    import android.graphics.BitmapFactory;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.graphics.PorterDuff.Mode;
    import android.graphics.PorterDuffXfermode;
    import android.net.Uri;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.widget.ImageView;
    public class MainActivity extends Activity {
    private ImageView mImg;
    private String uriImg="http://p4.so.qhmsg.com/sdr/534_768_/t01b32d0278850a58ae.jpg";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initView();
    threadTime();
    }
    private void initView() {
    mImg=(ImageView) findViewById(R.id.mImg);
    }
    private void threadTime(){
    new Thread(new Runnable() {

    @Override
    public void run() {
    try {
    URL url=new URL(uriImg);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    InputStream is = conn.getInputStream();
    final Bitmap bitmap = BitmapFactory.decodeStream(is);
    runOnUiThread(new Runnable() {

    @Override
    public void run() {
    //设置给图片
    mImg.setImageBitmap(getBitmap(bitmap));
    }
    });
    } catch (MalformedURLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }).start();
    }
    private static Bitmap getBitmap(Bitmap bitmap){
    //依据原有的图片丶创建一个新的图片   格式是:Config.ARGB_4444
    Bitmap bt=Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_4444);
    //创建一个画布
    Canvas canvas=new Canvas(bt);
    //创建一个画笔
    Paint paint=new Paint(Paint.ANTI_ALIAS_FLAG);
    //画笔的颜色
    paint.setColor(Color.WHITE);
    //画布的格式默认为  零
    canvas.drawARGB(0, 0, 0, 0);
    //求得圆的半径
    float radius = Math.min(bitmap.getWidth()/2, bitmap.getHeight()/2);
    canvas.drawCircle(bitmap.getWidth()/2, bitmap.getHeight()/2, radius, paint);
    //重置画笔
    paint.reset();
    //调用截图图层的方法
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    //画图片
    canvas.drawBitmap(bitmap, 0, 0, paint);
    return bt;
    }

    }

    //以下是xml布局:

    activity

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.example.roundbitmap_demo.MainActivity" >
        <ImageView
            android:id="@+id/mImg"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="fitXY" />
    </RelativeLayout>

    展开全文

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 2,324
精华内容 929
关键字:

android图片圆形