Search This Blog

Showing posts with label ImageView. Show all posts
Showing posts with label ImageView. Show all posts

Friday, September 22, 2017

Fix lổi không bo tròn khi dùng thư viện Glide

Bitmap bmp = ((GlideBitmapDrawable)holder.wallPaperImageView.getDrawable().getCurrent()).getBitmap();

Wednesday, July 12, 2017

Cập nhật src imageview

ImageView img = (ImageView)view.findViewById(R.id.tabImage) ;
img.setImageResource(R.drawable.icon_home);

Friday, June 23, 2017

Giảm dung lượng ảnh từ Bitmap

Bitmap b = BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length)
profileImage.setImageBitmap(Bitmap.createScaledBitmap(b, 120, 120, false));

Thursday, January 12, 2017

Cách mở thư viện ảnh và lấy địa chỉ ảnh

// su kien click nuimageViewAnhNguoiAy.setOnClickListener(new View.OnClickListener() {
    @Override    public void onClick(View v) {
        Intent intent = new Intent(Intent.ACTION_PICK);
        intent.setType("image/*");
        startActivityForResult(Intent.createChooser(intent,
                "Chọn ảnh"), SELECT_PICTURE);
    }
});

// hàm nhận kết quả chọn
@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == SELECT_PICTURE) {
            Uri selectedImageUri = data.getData();
            String imagePath = getPath(selectedImageUri);
            Toast.makeText(getApplicationContext(), "" + imagePath, Toast.LENGTH_LONG).show();
        }
    }
}

// hàm lấy đường dẫn
public String getPath(Uri uri) {
    if( uri == null ) {
        return null;
    }
    String[] projection = {
        MediaStore.Images.Media.DATA    };
    Cursor cursor =  getContentResolver().query(uri, projection, null, null, null);
    if( cursor != null ){
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        String path = cursor.getString(column_index);
        cursor.close();
        return path;
    }
    // this is our fallback here    return uri.getPath();
}

Tuesday, November 29, 2016

Imageview - Lấy cao rộng của ảnh

m_linImageStoreDP.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                // gets called after layout has been done but before display.
                if (Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1)
                m_linImageStoreDP.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                else
                m_linImageStoreDP.getViewTreeObserver().removeGlobalOnLayoutListener(this);

                mDisplaySize = new Point(m_linImageStoreDP.getWidth(), m_linImageStoreDP.getHeight());
               
            }
        });

Friday, November 11, 2016

Imageview - Cap nhat anh khac

m_imgStoreOC.setImageDrawable(getResources().getDrawable(R.drawable.unilever_background));

Friday, November 4, 2016

ImageView - Bo tròn ảnh

- Tạo class bo tròn  CircleImage.java :


public class CircleImage extends ImageView {

public CircleImage(Context context) {
super(context);
}

public CircleImage(Context context, AttributeSet attrs) {
super(context, attrs);
}

public CircleImage(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}

@Override
protected void onDraw(Canvas canvas) {

Drawable drawable = getDrawable();

if (drawable == null) {
return;
}

if (getWidth() == 0 || getHeight() == 0) {
return;
}
Bitmap b = ((BitmapDrawable) drawable).getBitmap();
Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true);

int w = getWidth();

Bitmap roundBitmap = getCroppedBitmap(bitmap, w);
canvas.drawBitmap(roundBitmap, 0, 0, null);

}

public static Bitmap getCroppedBitmap(Bitmap bmp, int radius) {
Bitmap sbmp;
if (bmp.getWidth() != radius || bmp.getHeight() != radius)
sbmp = Bitmap.createScaledBitmap(bmp, radius, radius, false);
else
sbmp = bmp;
Bitmap output = Bitmap.createBitmap(sbmp.getWidth(), sbmp.getHeight(),
Config.ARGB_8888);
Canvas canvas = new Canvas(output);

final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, sbmp.getWidth(), sbmp.getHeight());

paint.setAntiAlias(true);
paint.setFilterBitmap(true);
paint.setDither(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(Color.parseColor("#BAB399"));
canvas.drawCircle(sbmp.getWidth() / 2 + 0.7f,
sbmp.getHeight() / 2 + 0.7f, sbmp.getWidth() / 2 + 0.1f, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(sbmp, rect, rect, paint);

return output;
}
}
- class main.java :
public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}

- Xây dựng main.xml : 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical" >

    <minhnhut027.com.democircleimageview.CircleImage
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:src="@drawable/mn20161030003512" />

</RelativeLayout>

-  Trỏ đến địa chỉ ảnh : android:src="@drawable/mn20161030003512" 

- Kết quả : 






ImageView - Bo tròn ảnh trong imageview

- Nạp thư viện : 

dependencies {
    compile 'de.hdodenhof:circleimageview:1.1.0'
}
- Thiết kế xml :
<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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <TextView
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView" />

    <de.hdodenhof.circleimageview.CircleImageView
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/profile_image"
        android:layout_width="96dp"
        android:layout_height="96dp"
        android:src="@drawable/moneycat"
        app:border_width="2dp"
        app:border_color="#FF000000"
        android:layout_below="@+id/textView"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="84dp" />

</RelativeLayout>
- Kết quả : 

Friday, October 28, 2016

ImageView - Load ảnh động

    - Cai đặt thư viện : 

    - Gradle : 

dependencies {
    compile 'com.koushikdutta.ion:ion:2.+'
}
- Sử dụng : 
imageViewTime = (ImageView)findViewById(R.id.imageViewTime);
Ion.with(imageViewTime).load("http://media.7giftshop.com/media/wysiwyg/buom-mai-lac-lu-0211.gif");



- Tham khảo : https://github.com/koush/ion#get-ion















ImageView - Cập nhật chiều cao rộng của hình

image_view.getLayoutParams().height = 20;
image_view.getLayoutParams().width = 20;

Tuesday, July 12, 2016

ImageView - Sự kiện chạm vào ảnh

// sự kiện chạm vào ảnh
public static void SuKienChamVaoAnh(final ImageView imv,final String chuoi){
    imv.setOnTouchListener(new View.OnTouchListener() {
        @Override        
             public boolean onTouch(View v, MotionEvent event) {
            //imvBackActionBar.setBackgroundColor(Color.parseColor("#FF6E9BF5"));            
             switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN: {
                    ImageView view = (ImageView) v;
                    //overlay is black with transparency of 0x77 (119)                    
                    imv.setBackgroundColor(Color.parseColor("#FF6E9BF5"));
                    break;
                }
                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_CANCEL: {
                    ImageView view = (ImageView) v;
                    //clear the overlay                    
                    imv.setBackgroundColor(Color.parseColor(chuoi));
                    break;
                }
            }
            return false;
        }
    });
}

Tuesday, April 5, 2016

TEXTVIEW ON IMAGEVIEW


- FILE XML : 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    
android:orientation="vertical" 
android:layout_width="match_parent"    
android:layout_height="match_parent">

    <ImageView        
      android:layout_width="match_parent"        
      android:layout_height="200dp"        
      android:id="@+id/imageView2"        
      android:src="@drawable/phonglan"        
      android:layout_alignParentTop="true"        
      android:layout_alignParentLeft="true"        
      android:layout_alignParentStart="true"        
      android:layout_alignParentRight="true"        
      android:layout_alignParentEnd="true"
     />
    <LinearLayout        
      android:layout_width="match_parent"
      android:layout_height="wrap_content"        
      android:background="#5b38d6c3"        
      android:layout_gravity="center"        
      android:weightSum="1"        
      android:id="@+id/linearLayout"        
      android:orientation="horizontal"        
      android:layout_alignBottom="@+id/imageView2"        
      android:layout_alignParentLeft="true"        
      android:layout_alignParentStart="false"        
      android:layout_alignParentEnd="false"
     >
        <TextView  
          
          android:layout_width="match_parent"            
          android:layout_height="50dp"            
          android:textColor="#FF0000"            
          android:text="Le Minh Nhut"            
          android:gravity="center"            
          android:textSize="30"           
          android:layout_gravity="center"            
          android:layout_alignParentTop="true"            
          android:layout_alignParentLeft="true"            
          android:layout_alignParentStart="true"

         />
    </LinearLayout>

</RelativeLayout>