美文网首页
Fragment +Viewpager+RadioButton和

Fragment +Viewpager+RadioButton和

作者: Android_之父 | 来源:发表于2017-10-25 19:21 被阅读0次

首先定义布局文件

android:id="@+id/fl"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_above="@+id/rg"

>

android:id="@+id/rg"

android:layout_width="match_parent"

android:layout_height="50dp"

android:layout_alignParentBottom="true"

android:orientation="horizontal"

>

android:id="@+id/rb1"

android:layout_width="0dp"

android:layout_height="match_parent"

android:layout_weight="1"

android:button="@null"

android:text="主页"

android:gravity="center"

/>

android:id="@+id/rb2"

android:layout_width="0dp"

android:layout_height="match_parent"

android:layout_weight="1"

android:button="@null"

android:text="消息"

android:gravity="center"

/>

android:id="@+id/rb3"

android:layout_width="0dp"

android:layout_height="match_parent"

android:layout_weight="1"

android:button="@null"

android:text="其他"

android:gravity="center"

/>

实例化控件

private voidinitView() {

mRb1= (RadioButton) findViewById(R.id.rb1);

mRb2= (RadioButton) findViewById(R.id.rb2);

mRb3= (RadioButton) findViewById(R.id.rb3);

mRg= (RadioGroup) findViewById(R.id.rg);

mRg.check(R.id.rb1);

fragment_sy=newFragment_SY();

fragment_xx=newFragment_XX();

fragment_qt=newFragment_QT();

showFrag(R.id.fl,fragment_sy,"fragment_sy");

mRg.setOnCheckedChangeListener(newRadioGroup.OnCheckedChangeListener() {

@Override

public voidonCheckedChanged(RadioGroup radioGroup,inti) {

switch(i) {

caseR.id.rb1:

showFrag(R.id.fl,fragment_sy,"fragment_sy");

break;

caseR.id.rb2:

showFrag(R.id.fl,fragment_xx,"fragment_xx");

break;

caseR.id.rb3:

showFrag(R.id.fl,fragment_qt,"fragment_qt");

break;

default:

break;

}

}

});

}

//show方法

private voidshowFrag(intid,Fragment frag,String tag){

FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

Fragment fragment = getSupportFragmentManager().findFragmentByTag(tag);

if(fragment==null){

transaction.add(id,frag,tag);

}

List list=getSupportFragmentManager().getFragments();

for(Fragment f:list){

transaction.hide(f);

}

transaction.show(frag);

transaction.commit();

}

二次采样

/**

* @param filePath  要加载的图片路径

* @param destWidth  显示图片的控件宽度

* @param destHeight 显示图片的控件的高度

* @return

*/

public static android.graphics.Bitmap getBitmap(String filePath, int destWidth, int destHeight) {

//第一次采样

BitmapFactory.Options options = new BitmapFactory.Options();

//该属性设置为true只会加载图片的边框进来,并不会加载图片具体的像素点

options.inJustDecodeBounds = true;

//第一次加载图片,这时只会加载图片的边框进来,并不会加载图片中的像素点

BitmapFactory.decodeFile(filePath, options);

//获得原图的宽和高

int outWidth = options.outWidth;

int outHeight = options.outHeight;

//定义缩放比例

int sampleSize = 1;

while (outHeight / sampleSize > destHeight || outWidth / sampleSize > destWidth) {

//如果宽高的任意一方的缩放比例没有达到要求,都继续增大缩放比例

//sampleSize应该为2的n次幂,如果给sampleSize设置的数字不是2的n次幂,那么系统会就近取值

sampleSize *= 2;

}

/********************************************************************************************/

//至此,第一次采样已经结束,我们已经成功的计算出了sampleSize的大小

/********************************************************************************************/

//二次采样开始

//二次采样时我需要将图片加载出来显示,不能只加载图片的框架,因此inJustDecodeBounds属性要设置为false

options.inJustDecodeBounds = false;

//设置缩放比例

options.inSampleSize = sampleSize;

//加载图片并返回

return BitmapFactory.decodeFile(filePath, options);

}

相关文章

网友评论

      本文标题:Fragment +Viewpager+RadioButton和

      本文链接:https://www.haomeiwen.com/subject/guozsxtx.html