使图像完全无圆形无框

时间:2015-06-17 09:16:27

标签: java android xml image

我想要Profile活动的圆形图片。我已经尝试过这些答案,但在我的情况下它不起作用。

How to create a circular ImageView in Android?

How to make an ImageView with rounded corners?

How to make an image fit into a circular frame in android

How to Make an ImageView in Circular Shape?

这不是一个重复的问题,使用所有这些方法,这是我得到的: -

enter image description here

问题:图片不完全是圆形的。

这是我的Profile.java代码: -

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * Created by kundan on 6/4/2015.
 */
public class Profile extends ActionBarActivity {


    TextView username;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.profile);


        username= (TextView) findViewById(R.id.txt);

        String recievedusername=getIntent().getExtras().getString("toname");
        username.setText(recievedusername);


        Bitmap bm = BitmapFactory.decodeResource(getResources(),
                R.mipmap.gomez);
        Bitmap resized= Bitmap.createScaledBitmap(bm,200,200,true);
        Bitmap conv_bm=getCircleBitmap(resized,100);
        // set circle bitmap
        ImageView mImage = (ImageView) findViewById(R.id.profile_image);
        mImage.setImageBitmap(conv_bm);
        // TODO Auto-generated method stub
    }
    private Bitmap getCircleBitmap(Bitmap bitmap , int pixels) {
        final Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
                bitmap.getHeight(), Bitmap.Config.ARGB_8888);
        final 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());
        final RectF rectF = new RectF(rect);
        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawCircle(100, 100, 100, paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);
        bitmap.recycle();
        return output;
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_apploud, menu);
        return super.onCreateOptionsMenu(menu);
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        //noinspection SimplifiableIfStatement
        if (id == R.id.action_addnew) {
            Intent i;
            i=new Intent(Profile.this,ApplaudSomeone.class);
            startActivity(i);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

profile.xml的所需部分是: -

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    >
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:background="#ff17a088"
        android:weightSum="1"
        android:paddingTop="5dp"
        android:gravity="center">
        <ImageView
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:id="@+id/profile_image"
            android:contentDescription="@string/img"
            android:layout_gravity="center"
            android:scaleType="centerCrop"
            android:background="@drawable/img"
            android:layout_alignParentTop="true"
            android:layout_alignEnd="@+id/txt2" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="suman"
            android:id="@+id/txt"
            android:textSize="25sp"
            android:padding="5dp"
            android:layout_gravity="center_horizontal"
            android:textColor="#fffff9"
            android:layout_below="@+id/profile_image"
            android:layout_centerHorizontal="true" />
img.xml的代码是: -

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="oval"
            android:useLevel="false"
            >

            <stroke android:width="5dp"
                android:color="#ffffff"/>
        </shape>
    </item>
</layer-list>

先谢谢!!

1 个答案:

答案 0 :(得分:0)

是的,最后我解决了这个问题。如果您有任何人遇到同样的问题,那么使用canvas.drawCircle(100, 100, 90, paint);代替canvas.drawCircle(100, 100, 100, paint);这肯定会解决您的问题。