Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android組件:評級組件;textswither的text用.FrameLayout.LayoutParams,觸屏監聽;

android組件:評級組件;textswither的text用.FrameLayout.LayoutParams,觸屏監聽;

編輯:關於Android編程

評級界面

xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>


android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:rating="3"
android:numStars="5"
android:stepSize="0.5"
android:id="@+id/star"/>


點擊事件

ratingBar=(RatingBar)findViewById(R.id.star);
ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {

@Override
public void onRatingChanged(RatingBar ratingBar, float rating,
boolean fromUser) {
Toast.makeText(MainActivity.this,"得分:"+rating,Toast.LENGTH_LONG).show();
}
});


TextSwitcher文本切換,import android.widget.FrameLayout.LayoutParams;一般設置listview的寬高等參數用的是android.widget.AbsListView.LayoutParams

android:id="@+id/switcher"
android:layout_width="match_parent"
android:layout_height="match_parent" >

圖片切換

android:id="@+id/switcher"
android:layout_width="match_parent"
android:layout_height="match_parent" >

實現類都差不多

//switcher = (TextSwitcher) findViewById(R.id.switcher);
switcher = (ImageSwitcher) findViewById(R.id.switcher);

// 設置建立工廠類,返回顯示的內部組件
switcher.setFactory(new ViewFactory() {


@Override
public View makeView() {
// 建立要顯示文字的textview
/* TextView text = new TextView(MainActivity.this);
text.setTextColor(Color.BLACK);
text.setTextSize(16);
text.setBackgroundColor(Color.WHITE);
text.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
return text;*/
ImageView img=new ImageView(MainActivity.this);
img.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
return img;
}
});
// 處理內容
//switcher.setText(allTexts[index]);
switcher.setImageResource(allImgs[index]);

// 設置動畫效果, 系統動畫功能不完善,一般都滿足不了開發的需求, 因此這裡完成一個自定義的平移動畫
// switcher.setInAnimation(AnimationUtils.loadAnimation(this,
// android.R.anim.slide_in_left));
// switcher.setOutAnimation(AnimationUtils.loadAnimation(this,
// android.R.anim.slide_out_right));


// 加入觸屏監聽, 使用該監聽的前提是, 組件必須先加入過OnClickListener
switcher.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
}
});
switcher.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// 可以支持多點操作,但此程序只設置單點操作
if (event.getPointerCount() == 1) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// 手指剛到屏幕上
startX = event.getX();
startTime = System.currentTimeMillis();


} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
// 移動中
} else if (event.getAction() == MotionEvent.ACTION_UP) {
// 手指抬起
// 判斷當前橫坐標差,在一秒完成動作,並且滑行距離大於50
if (System.currentTimeMillis() - startTime < 1000) {
if (startX - event.getX() >= 50) {
// 向左滑的差,即下一頁
if (index < allImgs.length - 1) {
index++;
// 進入的動畫是滑動的 public TranslateAnimation
// (float fromXDelta, float toXDelta, float
// fromYDelta, float toYDelta)
Animation inAnim = new TranslateAnimation(
Globals.SCREEN_WIDTH + 10, 0, 0, 0);
Animation outAnim = new TranslateAnimation(
0, -Globals.SCREEN_WIDTH - 10, 0, 0);
// 設置動畫持續時間
inAnim.setDuration(1000);
outAnim.setDuration(1000);
switcher.setInAnimation(inAnim);
switcher.setOutAnimation(outAnim);
switcher.setImageResource(allImgs[index]);
}
} else if (event.getX() - startX >= 50) {
// 上一頁
if (index > 0) {
index--;


Animation inAnim = new TranslateAnimation(
-Globals.SCREEN_WIDTH - 10, 0, 0, 0);
Animation outAnim = new TranslateAnimation(
0, Globals.SCREEN_WIDTH + 10, 0, 0);
// 設置動畫持續時間
inAnim.setDuration(1000);
outAnim.setDuration(1000);
switcher.setInAnimation(inAnim);
switcher.setOutAnimation(outAnim);
switcher.setImageResource(allImgs[index]);
}
}
}
}


} else {
Toast.makeText(MainActivity.this, "支持單點操作",
Toast.LENGTH_SHORT).show();
}
return false;
}
});


contentresolver掃描sdcard查找視頻資源

// 根據一個唯一的標識,來調用某些數據庫操作.
Cursor c1 = resolver.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
null, null, null, null);


if (c1.moveToFirst()) {
while (!c1.isAfterLast()) {
String album = c1.getString(c1.getColumnIndex(Video.Media.ALBUM));
String artist = c1.getString(c1.getColumnIndex(Video.Media.ARTIST));
String fileName = c1.getString(c1.getColumnIndex(Video.Media.DISPLAY_NAME));
String name = c1.getString(c1.getColumnIndex(Video.Media.TITLE));
String duration = c.getString(c1.getColumnIndex(Video.Media.DURATION));
String path = c1.getString(c1.getColumnIndex(Video.Media.DATA));//路徑

MediaPlayer player = new MediaPlayer();

System.out.println(album + " --> " + artist + " --> " + name
+ " --> " + duration + " --> " + path + " --> "
+ fileName);


c1.moveToNext();
}
}
c1.close();

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved