我正在做一个需要根据搜索栏位置更改图像视图宽度的应用程序,我尝试使用下面的代码,但是当我移动搜索栏时,图像视图的宽度和高度都在变化,但我只想将宽度更改为改变,高度应该是恒定的。如果我在 xml 中为 imageview 设置背景只有宽度改变,但在我的应用程序中我必须动态设置背景,所以请任何人都可以帮助我....
public class MainActivity extends Activity implements OnSeekBarChangeListener {
float screenHeight,screenWidth,screendensity;
private SeekBar mSeekBar;
ImageView i1;
int width2;
RelativeLayout.LayoutParams parms,parms1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setNoTitle();
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
screenHeight = displaymetrics.heightPixels;
screenWidth = displaymetrics.widthPixels;
setContentView(R.layout.activity_main);
mSeekBar = (SeekBar) findViewById(R.id.seek_bar);
mSeekBar.setOnSeekBarChangeListener(this);
Globalclass.widthnew=(int)(212*(screenWidth/320));
Globalclass.hightnew=(int)(360*(screenHeight/480));
mSeekBar.setMax(Globalclass.widthnew);
mSeekBar.setProgress(Globalclass.widthnew);
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
width2=progress;
Toast.makeText(MainActivity.this, "Seekbar Value : " + width2, Toast.LENGTH_SHORT).show();
i1=(ImageView)findViewById(R.id.image);
i1.setImageResource(R.drawable.frame_one1);
parms = new RelativeLayout.LayoutParams(width2+50,Globalclass.hightnew);
parms.topMargin=(int)(200*(screenHeight/480));
i1.setLayoutParams(parms);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
Toast.makeText(MainActivity.this, "Started Tracking Seekbar", Toast.LENGTH_SHORT).show();
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
mSeekBar.setSecondaryProgress(seekBar.getProgress());
Toast.makeText(MainActivity.this, "Stopped Tracking Seekbar", Toast.LENGTH_SHORT).show();
}
private void setNoTitle() {
// TODO Auto-generated method stub
requestWindowFeature(Window.FEATURE_NO_TITLE);
}
}