android 常用UI使用工具代码片段

来源:互联网 发布:百度解压缩软件 编辑:程序博客网 时间:2024/05/16 00:41

/*

* 常用UI使用工具

*/

//***与像素密度无关的触控笔势

 

private static final float DISTANCE_DIP = 16.0f;

private static final float PATH_DIP = 40.0f;

// convert dip measurements to pixels

final float scale = getResources().getDisplayMetrics().density;

int scaledDistance = (int) (DISTANCE_DIP * scale + 0.5f);

int scaledPath = (int) (PATH_DIP * scale + 0.5f);

// For more information about touch gestures and screens support, see:

// http://developer.android.com/resources/articles/gestures.html

// http://developer.android.com/reference/android/gesture/package-summary.html

// http://developer.android.com/guide/practices/screens_support.html

 

//***缩放位图以查看大小

 

originalImage = Bitmap.createScaledBitmap( 

originalImage,     // bitmap to resize 

view.getWidth(),   // new width 

view.getHeight(),  // new height 

true);             // bilinear filtering

 

//***调整widget大小

 

public void onSizeChanged(int w, int h, int oldW,

int oldH) {

// Calculate relative sizes at runtime

// mButton and mButtonBackGround are of type Drawable

int selfW = mButton.getIntrinsicWidth();

int selfH = mButton.getIntrinsicHeight();

int marginX = (w - selfW) / 2;

int marginY = (h - selfH) / 2;

mButtonBackground.setBounds(marginX, marginY, 

marginX + selfW, marginY + selfH);

mButton.setBounds(marginX, marginY, 

marginX + selfW, marginY + selfH);

// Implement the measureText method to resize text data, if applicable

measureText();

}

 

//***响应widget交互

 

// Add import android.view.View.OnClickListener; statement

// Add import android.widget.Button; statement

// Add import android.view.View; statement

 

Button b1 = (Button) findViewById(R.id.your_button_id); // Use this method carefully, it consumes lots of system resources

b1.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

// Handle the button click here as you wish

}

});

原创粉丝点击