顯示具有 Activity 標籤的文章。 顯示所有文章
顯示具有 Activity 標籤的文章。 顯示所有文章

2016年9月4日 星期日

Android Activity Animation 頁面 動畫

頁面Activity與Dialog移動動畫效果

1.在res下建立anim資料夾

2.在anim建立left_1.xml, left_2.xml, right_1.xml, right_2.xml, up_1.xml, down_1.xml檔案
分別為向左移動,向右移動,向上移動與向下移動

3.left_1.xml

    

left_2.xml



right_1.xml

    

right_2.xml

    

up_1.xml

    

down_1.xml

    

4.Dialog需要在value/styles.xml加入
    
5.3個layout頁面
res/layout/activity_main.xml


    

    


res/layout/activity_next.xml


    

res/layout/dialog_layout.xml


    

6.MainActivity.java
package com.terryyamg.activityanimationtest;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btnNext = (Button) findViewById(R.id.btnNext);
        Button btnDialog = (Button) findViewById(R.id.btnDialog);
        btnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, NextActivity.class);
                startActivity(intent);
                overridePendingTransition(R.anim.right_1, R.anim.right_2);
            }
        });

        btnDialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final Dialog dialog = new Dialog(MainActivity.this);
                dialog.setContentView(R.layout.dialog_layout);
                dialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
                Button btnClose = (Button) dialog.findViewById(R.id.btnClose);
                btnClose.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        dialog.dismiss();
                    }
                });
                dialog.show();
            }
        });


    }
}

NextActivity.java
package com.terryyamg.activityanimationtest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

public class NextActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_next);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        overridePendingTransition(R.anim.left_1, R.anim.left_2);
    }

}



檔案下載:
 https://github.com/terryyamg/ActivityAnimationTest
參考來源:
http://stackoverflow.com/questions/26431017/how-to-achieve-right-to-left-animation-to-start-the-activity
http://android-er.blogspot.tw/2012/07/dialog-animation-using-windowanimations.html

2015年8月20日 星期四

Android Web Url Activity 網頁連結啟動app

點選網頁超連結來啟動app
1.AndroidManifest.xml


    

    
        
            
                

                
            
            
                

                
                

                
            
        
    


2.activity_main.xml


    
    


3.MainActivity.java
package tw.android;

import java.util.List;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {
 private TextView textView1;

 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  textView1 = (TextView) findViewById(R.id.textView1);

  Uri data = getIntent().getData();
  if (data != null) {
   String scheme = data.getScheme(); // "uccu"
   // String host = data.getHost();
   List<String> params = data.getPathSegments();
   String first = params.get(0); // word1
   String second = params.get(1); // word2

   textView1.setText(scheme + ":///" + first + "/" + second);
  }else{
   textView1.setText("請從網頁來");
  }
 }
}
4.web.html
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>

<a href="uccu:///word1/word2/">啟動吧!!APP!!!</a>

</body>
</html> 


參考來源:
http://stackoverflow.com/questions/2958701/launch-custom-android-application-from-android-browser

2015年2月10日 星期二

Android NFC Activity disable 關閉 近距離無線通訊

前篇有用到NFC使用方法,在可以使用NFC APP上,我們可能想要在一個頁面(Activity1)可以使用NFC,在下一個頁面(Activity2)不需要用到NFC,但是若沒有設定,仍會感應到tag資料,

所以需加上下列程式碼
/*此頁面關閉NFC感應*/
 @Override
 protected void onResume() {
  super.onResume();
  // creating pending intent:
  PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
    new Intent(this, getClass())
      .addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
  // enabling foreground dispatch for getting intent from NFC event:
  NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
  try {
//   nfcAdapter.enableForegroundDispatch(this, pendingIntent,
//     new IntentFilter[] { filter }, this.techList);  開啟nfc
   nfcAdapter.enableForegroundDispatch(this, pendingIntent,
     new IntentFilter[] {}, null); // 關閉nfc IntentFilter[] {}內設為空值, techList改為null空值
  } catch (NullPointerException e) {
  }
 }
參考資料: http://stackoverflow.com/questions/9748513/reading-nfc-tags-only-from-a-particuar-activity