裁剪方形图像圈 - 编程 [英] Crop square image to circle - Programmatically

查看:149
本文介绍了裁剪方形图像圈 - 编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找过去的一天,我是不会成功的。

我从API的形象,我用下面的code将其下载到一个位图文件。

 私人位图DownloadImage(字符串URL)
    {
        点阵位图= NULL;
        在的InputStream = NULL;
        尝试
        {
            在= OpenHttpConnection(URL);
            位= BitmapFactory.de codeStream(中);
            附寄();
        }
        赶上(IOException异常E1)
        {
            e1.printStackTrace();
        }
        返回的位图;
    }

    私人的InputStream OpenHttpConnection(字符串urlString)抛出IOException异常
    {
        在的InputStream = NULL;
        INT响应= -1;

        网址URL =新的URL(urlString);
        URLConnection的康恩= url.openConnection();

        如果(!(康涅狄格州的instanceof的HttpURLConnection))
            抛出新的IOException异常(不是一个HTTP连接);

        尝试
        {
            HttpURLConnection的httpConn =(HttpURLConnection类),康涅狄格州;
            httpConn.setAllowUserInteraction(假);
            httpConn.setInstanceFollowRedirects(真正的);
            httpConn.setRequestMethod(GET);
            httpConn.connect();

            响应= httpConn.getResponse code();
            如果(响应== HttpURLConnection.HTTP_OK)
            {
                在= httpConn.getInputStream();
            }
        }
        赶上(例外前)
        {
            抛出新的IOException异常(错误连接);
        }
        返回;
    }
 

和我得到的图像作为一个正方形,我要裁剪的四个角落,并使其圆形图像。有没有取得任何可能的方式?

任何相关的答案,欢迎。先谢谢了。

解决方案

 公共类MainActivity延伸活动{
    @覆盖
    保护无效的onCreate(包savedInstanceState){
        // TODO自动生成方法存根
        super.onCreate(savedInstanceState);
        DrawingView DV =新DrawingView(本);
        的setContentView(DV);
    }

    类DrawingView扩展视图{
        点阵位图;

        公共DrawingView(上下文的背景下){
            超(上下文);
            位= BitmapFactory.de codeResource(context.getResources()
                    R.drawable.glossy_overlay);

        }

        @覆盖
        公共无效的OnDraw(帆布油画){
            涂料粉刷=新的油漆();
            // paint.setColor(Color.CYAN);
            canvas.drawBitmap(getclip(),30,20,油漆);
        }

        公共位图getclip(){
            位图输出= Bitmap.createBitmap(bitmap.getWidth()
                    bitmap.getHeight(),Config.ARGB_8888);
            帆布油画=新的Canvas(输出);
            最终诠释色= 0xff424242;
            最终的涂料粉刷=新的油漆();
            最终矩形矩形=新的Rect(0,0,bitmap.getWidth(),
                    bitmap.getHeight());

            paint.setAntiAlias​​(真正的);
            canvas.drawARGB(0,0,0,0);
            // paint.setColor(彩色);
            canvas.drawCircle(bitmap.getWidth()/ 2,
                    bitmap.getHeight()/ 2,bitmap.getWidth()/ 2,油漆);
            paint.setXfermode(新PorterDuffXfermode(Mode.SRC_IN));
            canvas.drawBitmap(位图,矩形,矩形,油漆);
            返回输出;
        }
    }
}
 

i was searching for past one day and i was not successful .

i get the image from API , and i download it to a bitmap file using the following code .

private Bitmap DownloadImage(String URL) 
    {
        Bitmap bitmap = null;
        InputStream in = null;
        try 
        {
            in = OpenHttpConnection(URL);
            bitmap = BitmapFactory.decodeStream(in);
            in.close();
        }
        catch (IOException e1) 
        {
            e1.printStackTrace();
        }
        return bitmap;
    }

    private InputStream OpenHttpConnection(String urlString) throws IOException 
    {
        InputStream in = null;
        int response = -1;

        URL url = new URL(urlString);
        URLConnection conn = url.openConnection();

        if (!(conn instanceof HttpURLConnection))
            throw new IOException("Not an HTTP connection");

        try 
        {
            HttpURLConnection httpConn = (HttpURLConnection) conn;
            httpConn.setAllowUserInteraction(false);
            httpConn.setInstanceFollowRedirects(true);
            httpConn.setRequestMethod("GET");
            httpConn.connect();

            response = httpConn.getResponseCode();
            if (response == HttpURLConnection.HTTP_OK) 
            {
                in = httpConn.getInputStream();
            }
        }
        catch (Exception ex) 
        {
            throw new IOException("Error connecting");
        }
        return in;
    }

And i get the image as a square and i want to crop the four corners and make it to circular image . Is there any possible way to achieve ?

Any related answers are welcomed . Thanks in advance .

解决方案

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        DrawingView dv = new DrawingView(this);
        setContentView(dv);
    }

    class DrawingView extends View {
        Bitmap bitmap;

        public DrawingView(Context context) {
            super(context);
            bitmap = BitmapFactory.decodeResource(context.getResources(),
                    R.drawable.glossy_overlay);

        }

        @Override
        public void onDraw(Canvas canvas) {
            Paint paint = new Paint();
            // paint.setColor(Color.CYAN);
            canvas.drawBitmap(getclip(), 30, 20, paint);
        }

        public Bitmap getclip() {
            Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
                    bitmap.getHeight(), Config.ARGB_8888);
            Canvas canvas = new Canvas(output);
            final int color = 0xff424242;
            final Paint paint = new Paint();
            final Rect rect = new Rect(0, 0, bitmap.getWidth(),
                    bitmap.getHeight());

            paint.setAntiAlias(true);
            canvas.drawARGB(0, 0, 0, 0);
            // paint.setColor(color);
            canvas.drawCircle(bitmap.getWidth() / 2,
                    bitmap.getHeight() / 2, bitmap.getWidth() / 2, paint);
            paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
            canvas.drawBitmap(bitmap, rect, rect, paint);
            return output;
        }
    }
}

这篇关于裁剪方形图像圈 - 编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆