动态字幕的文本

时间:2012-12-03 09:23:58

标签: android android-listview textview android-xml android-scrollview

是否可以将列表视图的java编码中的文本添加到Android中的选框滚动。如果是的话,请告诉我怎么做?

如果需要,我将发布使用的代码。 这是使用的listview的XML如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
     <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
    </LinearLayout>
    <ListView
        android:id="@+id/audiolist_listView"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_gravity="bottom"
        android:layout_weight="1"
        android:background="@drawable/backgroundimg"
        android:divider="@drawable/gradient"
        android:dividerHeight="1dp"
        android:scrollbars="vertical" >
    </ListView>

</LinearLayout>

listview的java类如下

public class Audiovediolist extends Activity implements OnItemClickListener {
    private ListView audioList;
    private Intent frmHome;
    private ArrayList<HashMap<String, String>> dataList;
    private HashMap<String, String> map;
    private HashMap<String, String> data;
    private int Screen_Id;
    private AdView adView;
    String name;
    String mSign[] = { "Aries-Mesam", "Taurus-Vrushabham", "Gemini-Midhunam",
            "Cancer-Karkatakam", "Leo-Simham", "Virgo-Kanya", "Libra-Tula",
            "Scorpio-Vruchikam", "Sagittarius-Dhanussu", "Capricorn-Makaram",
            "Aquarius-Kumbham", "Pisces-Meenam" };
    Integer mImages[] = { R.drawable.meshamu, R.drawable.vrushabhamu,
            R.drawable.medhunam, R.drawable.karkatakam, R.drawable.simham,
            R.drawable.kanya, R.drawable.tula, R.drawable.vruchikam,
            R.drawable.dhanussu, R.drawable.makaramu, R.drawable.kumbhamu,
            R.drawable.meenamu };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_RIGHT_ICON);
        setContentView(R.layout.list);
        frmHome = getIntent();
        getIntentValues();
        initUI();
        setUI();
        uiListener();
    }

    private void initUI() {
        audioList = (ListView) findViewById(R.id.audiolist_listView);
        dataList = new ArrayList<HashMap<String, String>>();
        adView = new AdView(this, AdSize.SMART_BANNER, "a150b89c23af3b2");
        LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout1);
        layout.setGravity(Gravity.TOP);
        layout.addView(adView);
        adView.loadAd(new AdRequest());
    }

    private void uiListener() {
        audioList.setOnItemClickListener(this);
    }

    private void getIntentValues() {
        String id = frmHome.getStringExtra("Activity_Id");
        Screen_Id = Integer.parseInt(id);
        if (Screen_Id == 1) {
            getWindow().setFeatureDrawableResource(Window.FEATURE_RIGHT_ICON,
                    R.drawable.audioo);
            this.setTitle(getResources().getString(R.string.audio));
            this.setTitleColor(getResources().getColor(R.color.Beige));
        } else {
            this.setTitle(getResources().getString(R.string.video));
            this.setTitleColor(getResources().getColor(R.color.Beige));
            getWindow().setFeatureDrawableResource(Window.FEATURE_RIGHT_ICON,
                    R.drawable.videoo);
        }
    }

    private void setAdapter() {
        SimpleAdapter adapter = new SimpleAdapter(this, dataList,
                R.layout.list_row, new String[] { "Image", "text" }, new int[] {
                        R.id.audiolist_row_img, R.id.audiolist_row_tv });
        audioList.setAdapter(adapter);
    }

    private void setUI() {
        for (int a = 0; a <= 11; a++) {
            map = new HashMap<String, String>();
            map.put("Image", "" + mImages[a]);
            map.put("text", mSign[a]);
            dataList.add(map);
            map = null;
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        setAdapter();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        System.gc();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        adView.destroy();
    }

    /*
     * private class NextTask extends AsyncTask<Void,Void,Void> { ProgressDialog
     * progressDailog = new ProgressDialog(Audiovediolist.this);
     * 
     * @Override protected void onPreExecute() { super.onPreExecute();
     * progressDailog.setTitle("Please Wait");
     * progressDailog.setMessage("URL Data Loading ...");
     * progressDailog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
     * progressDailog.setCancelable(true); progressDailog.show(); }
     * 
     * @SuppressWarnings("static-access")
     * 
     * @Override protected Void doInBackground(Void... params) { return null; }
     * 
     * @Override protected void onPostExecute(Void result) {
     * progressDailog.dismiss(); } }
     */
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        data = dataList.get(position);
        //FinalConstants.AUDIO_VIDEO = "list";
        try {
            NetWorkCheck netWork = new NetWorkCheck();
            if (netWork.isNetWorkConnection(this)) {
                if (Screen_Id == 1) {
                    // new NextTask().execute();
                    Intent audioInt=new  Intent(getApplicationContext(),AudioView.class);
                     audioInt.setType(data.get("text"));

                     startActivity(audioInt);
                } else if (Screen_Id == 2) {
                    Intent vedioInt = new Intent(this, VideoPlayActivity.class);
                    vedioInt.setType(data.get("text"));
                    startActivity(vedioInt);
                }
            } else {
                netWork.alert();
            }
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(this, "Problem while ListItemClick",
                    Toast.LENGTH_SHORT).show();
        }
    }
}

我需要选择滚动的文字的音频播放器代码如下

public class AudioView extends Activity implements OnClickListener {
    private MediaPlayer mMediaPlayer;
    private ImageView mPlay;
    private ImageView mPause;
    private ImageView mBack;
    private ImageView mFar;
    private ProgressBar myProgressBar;
    private boolean boolFlag = false;
    private boolean isPausedInCall = false;
    private String url;
    private TelephonyManager mTelephoneMgr;
    private NotificationManager mNotificationManager;
    private int SIMPLE_NOTFICATION_ID;
    private TextView tv;
    // private boolean serviceRunning;
    Notification notifyDetails;
    private AdView adView;


    @SuppressWarnings("deprecation")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        setContentView(R.layout.marquee);
        Toast.makeText(this,"please wait.........",Toast.LENGTH_LONG).show();
        url=getIntent().getType();
        initUI();
        uiListener();

        mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notifyDetails = new Notification(R.drawable.mulug_icon, url,
                System.currentTimeMillis());
        mTelephoneMgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        if (mTelephoneMgr != null) {
            mTelephoneMgr.listen(phoneStateListener,
                    PhoneStateListener.LISTEN_CALL_STATE);
        }
        /*if(FinalConstants.AUDIO_VIDEO=="listitem"){
        LoginProgress task = new LoginProgress();
        task.applicationContext = AudioView.this;
        task.execute();
    }*/
    }

    private void initUI() {
        tv = (TextView) findViewById(R.id.text);
        mBack = (ImageView) findViewById(R.id.back);
        mPlay = (ImageView) findViewById(R.id.play);
        mPause = (ImageView) findViewById(R.id.pause);
        mFar = (ImageView) findViewById(R.id.fwd);
        myProgressBar = (ProgressBar) findViewById(R.id.progressbar_Horizontal);
        myProgressBar.setProgressDrawable(getResources().getDrawable(
                R.drawable.green_progress));
        myProgressBar.setProgress(0);
        adView = new AdView(this, AdSize.BANNER, "a150b89c23af3b2");
        LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout1);
        layout.setGravity(Gravity.TOP);
        layout.addView(adView);
        adView.loadAd(new AdRequest());
        tv.setText(url + "more Deails Click on www.mulugu.com");
        tv.setSelected(true);
    }
    private void uiListener() {
        mPlay.setOnClickListener(this);
        mPlay.setOnClickListener(this);
        mPause.setOnClickListener(this);
        mPause.setVisibility(android.view.View.INVISIBLE);
        mBack.setOnClickListener(this);
        mFar.setOnClickListener(this);
    }

    private Thread myThread = new Thread() {
        public void run() {
            while (mMediaPlayer.getCurrentPosition() < mMediaPlayer
                    .getDuration()) {
                try {
                    myProgressBar
                            .setProgress(mMediaPlayer.getCurrentPosition());
                } catch (Throwable t) {
                }
            }
        }
    };



    @Override
    protected void onStart() {
        super.onStart();
        this.setProgressBarIndeterminateVisibility(true);
        try {
            if (boolFlag == false) {
                System.out.println(FinalConstants.URL + "mobile-audio/" + url+ ".mp3");
                mMediaPlayer = MediaPlayer.create(
                        this,
                        Uri.parse(FinalConstants.URL + "mobile-audio/" + url
                                + ".mp3"));
            }
            myProgressBar.setMax(mMediaPlayer.getDuration());
        } catch (Exception e) {
            Toast.makeText(getApplicationContext(), "Data not available",
                    Toast.LENGTH_SHORT).show();
            Log.e("ERRORS", "Data Not Exist in that Url...");
            super.onBackPressed();
            e.printStackTrace();
        }
    }

    @SuppressLint("NewApi")
    @Override
    public void onBackPressed() {
        super.onBackPressed();
        mMediaPlayer.stop();
        // myThread.stop();
        mNotificationManager.cancel(SIMPLE_NOTFICATION_ID);
        System.gc();
    }

    @Override
    protected void onDestroy() {
        adView.destroy();
        super.onDestroy();
        System.gc();
    }



    public PhoneStateListener phoneStateListener = new PhoneStateListener() {
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            if (state == TelephonyManager.CALL_STATE_RINGING) {
                System.out.println("ringing state");
                if (mMediaPlayer != null) {
                    pauseMedia();
                    isPausedInCall = true;
                }
                notificationShow();
            }
            if (state == TelephonyManager.CALL_STATE_IDLE) {
                System.out.println("callState Idle");
                if (mMediaPlayer != null) {
                    if (isPausedInCall) {
                        isPausedInCall = false;
                        playMedia();
                    }
                }
            }
            if (state == TelephonyManager.CALL_STATE_OFFHOOK) {
            }
            super.onCallStateChanged(state, incomingNumber);
        }
    };

    private void pauseMedia() {
        mMediaPlayer.pause();
        changePlayerIcons(false);
    }

    private void playMedia() {
        mMediaPlayer.start();
        changePlayerIcons(true);
    }
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.play:
            if (!boolFlag) {
                try {
                    notificationShow();
                     mMediaPlayer.setLooping(false);
                     mMediaPlayer.prepareAsync();
                } catch (Exception e) {
                    Toast.makeText(this, "preparing", Toast.LENGTH_SHORT)
                            .show();
                }
                boolFlag = true;
                mMediaPlayer.start();
                myThread.start();
            } else {
                notificationShow();
                mMediaPlayer.getCurrentPosition();
                mMediaPlayer.start();
            }
            changePlayerIcons(true);
            myProgressBar.setProgress(mMediaPlayer.getCurrentPosition());
            mMediaPlayer.setOnCompletionListener(new OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer arg0) {
                    changePlayerIcons(false);
                    myProgressBar.setProgress(0);
                    onStart();
                }
            });
            mMediaPlayer.setOnPreparedListener(new OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                }
            });
            break;
        case R.id.pause:
            mMediaPlayer.pause();
            mPlay.setVisibility(android.view.View.VISIBLE);
            mPause.setVisibility(android.view.View.INVISIBLE);
            break;
        case R.id.back:
            int dur = mMediaPlayer.getCurrentPosition();
            int pos = (dur > 10000 ? dur - 5000 : 0);
            mMediaPlayer.seekTo(pos);
            break;
        case R.id.fwd:
            int curpos = mMediaPlayer.getCurrentPosition();
            int dur2 = mMediaPlayer.getDuration();
            int pos2 = (curpos + 5000 > dur2 ? dur2 : curpos + 5000);
            mMediaPlayer.seekTo(pos2);
            break;
        }
    }

    @SuppressWarnings("deprecation")
    private void notificationShow() {
        Context context = getApplicationContext();
        CharSequence contentTitle = "Mulugu Running";
        CharSequence contentText = url + " Weekly Predictions";

        Intent notifyIntent = new Intent(AudioView.this, AudioView.class);
        PendingIntent intent = PendingIntent.getActivity(AudioView.this, 0,
                notifyIntent, android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP);
        notifyDetails.setLatestEventInfo(context, contentTitle, contentText,
                intent);
        mNotificationManager.notify(SIMPLE_NOTFICATION_ID, notifyDetails);
    }

    private void changePlayerIcons(boolean b) {
        if (b) {
            mPlay.setVisibility(android.view.View.INVISIBLE);
            mPause.setVisibility(android.view.View.VISIBLE);
        } else {
            mPlay.setVisibility(android.view.View.VISIBLE);
            mPause.setVisibility(android.view.View.INVISIBLE);
        }
    }

    /*public class LoginProgress extends AsyncTask<Void, Void, Void> {

        private ProgressDialog dialog;
        protected Context applicationContext;

        @Override
        protected void onPostExecute(Void result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            this.dialog.dismiss();
        }

        @Override
        protected void onPreExecute() {
            this.dialog = ProgressDialog.show(applicationContext, "Data Loading",
                    "Loading .....", true);
        }

        @Override
        protected Void doInBackground(Void... params) {
            LoadUrl();
            return null;
        }
        private void LoadUrl() {
            mMediaPlayer = new MediaPlayer();
            mMediaPlayer.reset();
            try {
                mMediaPlayer.setDataSource(AudioView.this,Uri.parse(FinalConstants.URL + "mobile-audio/" + getIntent().getType()+ ".mp3"));
                mMediaPlayer.setLooping(false); // Set looping
                mMediaPlayer.prepare();
                myProgressBar.setMax(mMediaPlayer.getDuration());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }*/
}

4 个答案:

答案 0 :(得分:3)

如果你需要移动文字,也许这会有所帮助:

http://developer.android.com/guide/topics/resources/animation-resource.html#translate-element

如果你的TextView文本太大并且你想让它移动以便人们可以读取整个内容,请尝试.xml文件中的android:ellipsize="marquee"或Java代码: textView.setEllipsize(TextUtils.TruncateAt.MARQUEE);

答案 1 :(得分:1)

如果android库不够,你可以随时使用java编码。创建一个线程类,用于获取视图并随时更改条件。您可以创建一个调用该类的意图。

答案 2 :(得分:1)

您可以使用 TextSwitcher 并在其上应用动画。

尝试以下代码:

public class TextSwitcherActivity extends Activity implements ViewSwitcher.ViewFactory,
    View.OnClickListener                    
  {
private TextSwitcher m_switcher;
private Button m_nextButton;
private int m_counter = 0;

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

    m_switcher = (TextSwitcher) findViewById(R.id.tsSwitcher);
    m_switcher.setFactory(this);

    Animation in = AnimationUtils.loadAnimation(this, android.R.anim.fade_in);
    Animation out = AnimationUtils.loadAnimation(this, android.R.anim.fade_out);
    m_switcher.setInAnimation(in);
    m_switcher.setOutAnimation(out);

    m_nextButton = (Button) findViewById(R.id.next);
    m_nextButton.setOnClickListener(this);
    updateCounter();
}

/**
 * Update counter value
 */
public void onClick(View v)
{
    m_counter++;
    updateCounter();
}

/**
 * Set text on textswitcher
 */
private void updateCounter()
{
    m_switcher.setText(String.valueOf(m_counter));
}

public View makeView()
{
    TextView t = new TextView(this);
    t.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
    t.setTextSize(36);
    return t;
}

您可以定义自己的动画以获得选框效果。

答案 3 :(得分:0)

以下是示例

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
    android:id="@+id/mywidget"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:lines="1"
    android:ellipsize="marquee"
    android:fadingEdge="horizontal"
    android:marqueeRepeatLimit="marquee_forever"
    android:scrollHorizontally="true"
    android:textColor="#ff4500"
    android:text="Simple application that shows how to use marquee, with a long text" />

在java中(如果您正在使用Listview并绑定适配器,那么当您对Layout及其视图组件进行充气时,在getview中,将所选标志设置为true到TextView。)

tv = (TextView) this.findViewById(R.id.tv);  
    tv.setSelected(true);