7pupu / android-studio

android-studio
0 stars 0 forks source link

180817 在ConstraintLayout 動態新增imageview #3

Open wotupset opened 6 years ago

wotupset commented 6 years ago
      //iv = (ImageView) findViewById(R.id.imageView);
        iv = new ImageView(MainActivity.this);
        ConstraintLayout.LayoutParams params = new ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        //new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
        iv.setLayoutParams(params);  //设置图片宽高

        bmp=BitmapFactory.decodeResource(getResources(),R.drawable.p2);//image is your image
        double dd= getResources().getDisplayMetrics().density;
        bmp=Bitmap.createScaledBitmap(bmp, (int)(1.0*200*dd),(int)(1.0*200*dd), true);
        iv.setImageBitmap(bmp);

        ConstraintLayout mylayout = (ConstraintLayout) findViewById(R.id.layout_id);
        mylayout.addView(iv);

在ConstraintLayout 動態新增imageview https://github.com/7pupu/android-studio/blob/master/180817/MainActivity.java

wotupset commented 6 years ago

參考官方文件 https://developer.android.com/reference/android/support/constraint/ConstraintLayout.LayoutParams