2016-06-15 193 views
0

这里我想创建一个circleimageview,并从它设置图像从gallarIn我创建一个circleimageview 如何从gallary设置图像在circleimage视图?如何在CircleImageView上点击图片时设置图片?

我的XML文件

<de.hdodenhof.circleimageview.CircleImageView 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     android:id="@+id/profile_image" 
     android:layout_width="76dp" 
     android:layout_height="76dp" 
     android:src="@drawable/profile" 
    </de.hdodenhof.circleimageview.CircleImageView> 

在这里,我创建circeimageview 的对象,并通过意图从GALLARY

选择图像在Java文件

 profileImage =(de.hdodenhof.circleimageview.CircleImageView)findViewById(R.id.profile_image); 
    profileImage.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      Toast.makeText(getApplicationContext(), "Profile Pic",  Toast.LENGTH_SHORT).show(); 
      Intent intent = new Intent(); 
      intent.setType("image/*"); 
      intent.setAction(Intent.ACTION_GET_CONTENT);// 
      startActivityForResult(Intent.createChooser(intent, "Select File"),SELECT_FILE); 
     } 
    }); 

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (resultCode == Activity.RESULT_OK) { 
     if (requestCode == SELECT_FILE) 
      onSelectFromGalleryResult(data); 
    } 
} 
@SuppressWarnings("deprecation") 
private void onSelectFromGalleryResult(Intent data) { 
    Bitmap bm=null; 
    if (data != null) { 
     try { 
      bm = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), data.getData()); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
    profileImage.setImageBitmap(bm); 
} 

帮我解决此问题。

想创建带有圆形图像视图的个人头像。

我发现这么多的教程,但有没有onclick事件

回答

1
private void onSelectFromGalleryResult(Intent data) { 
    if (data != null) { 
      Uri selectedImage = data.getData(); 
      String[] filePathColumn = { MediaStore.Images.Media.DATA }; 

      Cursor cursor = getContentResolver().query(selectedImage, 
        filePathColumn, null, null, null); 
      cursor.moveToFirst(); 

      int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
      String picturePath = cursor.getString(columnIndex); 
      cursor.close(); 
      profileImage.setImageBitmap(BitmapFactory.decodeFile(picturePath)); 
     } 
} 
+0

谢谢,这帮助了我很多 –

+0

的欢迎:)快乐编码:) –

0

如果你正在寻找如何将图像转换成圆形的尝试毕加索库很容易使用,你可以使用循环改造设置成。谷歌是图像视图你会发现很多的例子

+0

谢谢你,但不想使用毕加索。 –

0

嘿,我找到了答案终于

<de.hdodenhof.circleimageview.CircleImageView 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/profile_image" 
    android:layout_width="76dp" 
    android:layout_height="76dp" 
    android:src="@drawable/profile" 
    android:clickable="true" 
    android:layout_marginLeft="24dp" 
    android:layout_centerVertical="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:layout_marginStart="24dp"> 

</de.hdodenhof.circleimageview.CircleImageView> 

这里,

机器人:可点击= “真”

解决我的问题

感谢所有

+0

感谢所有重播 –