Android轮播问题

时间:2014-05-08 17:40:08

标签: android android-assets

我是这里的新手,想知道是否有人可以帮我解决这个问题。我能够访问这个网站http://www.pocketmagic.net/2013/06/a-3d-carousel-view-for-android/#.U2fTONK-o-U以获得一个3D轮播代码的示例,但是当我在我的模拟器上运行它时,它确实有效,但是没有显示它想要复制的图像。资产文件夹到SD卡。

我已经上传了他的文件和我自己的更改给你们看看并帮助我。我需要帮助的唯一一条线是我有评论说"将文件从资产文件夹复制到SD卡"。

他的MainActivity.java文件如下:

package net.pocketmagic.android.carousel;
/*
 * 3D carousel View
 * http://www.pocketmagic.net 
 *
 * Copyright (c) 2013 by Radu Motisan , radu.motisan@gmail.com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * For more information on the GPL, please go to:
 * http://www.gnu.org/copyleft/gpl.html
 *
 */ 
import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.support.v4.app.NavUtils;
import android.text.Editable;
import android.text.TextWatcher;

public class MainActivity extends Activity implements OnItemSelectedListener, TextWatcher {

    Singleton               m_Inst                  = Singleton.getInstance();
    CarouselViewAdapter     m_carouselAdapter       = null;  
    private final int       m_nFirstItem            = 1000;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //no keyboard unless requested by user
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 

        // compute screen size and scaling
        Singleton.getInstance().InitGUIFrame(this);

        int padding = m_Inst.Scale(10);
        // create the interface : full screen container
        RelativeLayout panel  = new RelativeLayout(this);
        panel.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
        panel.setPadding(padding, padding, padding, padding);
        panel.setBackgroundDrawable(new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, 
                new int[]{Color.WHITE, Color.GRAY}));
        setContentView(panel); 

        // copy images from assets to sdcard
        AppUtils.AssetFileCopy(this, "/mnt/sdcard/plasma1.png", "plasma1.png", false);
        AppUtils.AssetFileCopy(this, "/mnt/sdcard/plasma2.png", "plasma2.png", false);
        AppUtils.AssetFileCopy(this, "/mnt/sdcard/plasma3.png", "plasma3.png", false);
        AppUtils.AssetFileCopy(this, "/mnt/sdcard/plasma4.png", "plasma4.png", false);

        //Create carousel view documents
        ArrayList<CarouselDataItem> Docus = new ArrayList<CarouselDataItem>();
        for (int i=0;i<1000;i++) {
            CarouselDataItem docu;
            if (i%4==0) docu = new CarouselDataItem("/mnt/sdcard/plasma1.png", 0, "First Image "+i);
            else if (i%4==1) docu = new CarouselDataItem("/mnt/sdcard/plasma2.png", 0, "Second Image "+i);
            else if (i%4==2) docu = new CarouselDataItem("/mnt/sdcard/plasma3.png", 0, "Third Image "+i);
            else docu = new CarouselDataItem("/mnt/sdcard/plasma4.png", 0, "4th Image "+i);
            Docus.add(docu);
        } 

        // add the serach filter
        EditText etSearch = new EditText(this);
        etSearch.setHint("Search your documents");
        etSearch.setSingleLine();
        etSearch.setTextColor(Color.BLACK);
        etSearch.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_menu_search, 0, 0, 0);
        AppUtils.AddView(panel, etSearch, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 
                new int[][]{new int[]{RelativeLayout.CENTER_HORIZONTAL}, new int[]{RelativeLayout.ALIGN_PARENT_TOP}}, -1,-1);
        etSearch.addTextChangedListener((TextWatcher) this); 

        // add logo
        TextView tv = new TextView(this);
        tv.setTextColor(Color.BLACK);
        tv.setText("www.pocketmagic.net");
        AppUtils.AddView(panel, tv, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 
                new int[][]{new int[]{RelativeLayout.CENTER_HORIZONTAL}, new int[]{RelativeLayout.ALIGN_PARENT_BOTTOM}}, -1,-1);

        // create the carousel
        CarouselView coverFlow = new CarouselView(this);

        // create adapter and specify device independent items size (scaling)
        // for more details see: http://www.pocketmagic.net/2013/04/how-to-scale-an-android-ui-on-multiple-screens/
        m_carouselAdapter =  new CarouselViewAdapter(this,Docus, m_Inst.Scale(400),m_Inst.Scale(300));
        coverFlow.setAdapter(m_carouselAdapter);
        coverFlow.setSpacing(-1*m_Inst.Scale(150));
        coverFlow.setSelection(Integer.MAX_VALUE / 2, true);
        coverFlow.setAnimationDuration(1000);
        coverFlow.setOnItemSelectedListener((OnItemSelectedListener) this);

        AppUtils.AddView(panel, coverFlow, LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT, 
                new int[][]{new int[]{RelativeLayout.CENTER_IN_PARENT}},
                -1, -1); 
    }

    public void afterTextChanged(Editable arg0) {}

    public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

    public void onTextChanged(CharSequence s, int start, int before, int count) {
        m_carouselAdapter.getFilter().filter(s.toString()); 
    }

    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
         CarouselDataItem docu =  (CarouselDataItem) m_carouselAdapter.getItem((int) arg3);
         if (docu!=null)
             Toast.makeText(this, "You've clicked on:"+docu.getDocText(), Toast.LENGTH_SHORT).show();
    }

    public void onNothingSelected(AdapterView<?> arg0) {}



}

这是我的:

package com.src.carouseltest;

import java.util.ArrayList;

import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.support.v4.app.NavUtils;
import android.text.Editable;
import android.text.TextWatcher;

public class MainActivity extends Activity implements OnItemSelectedListener, TextWatcher {

    Singleton               m_Inst                  = Singleton.getInstance();
    CarouselViewAdapter     m_carouselAdapter       = null;  
    private final int       m_nFirstItem            = 1000;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //no keyboard unless requested by user
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 

        // compute screen size and scaling
        Singleton.getInstance().InitGUIFrame(this);

        int padding = m_Inst.Scale(10);
        // create the interface : full screen container
        RelativeLayout panel  = new RelativeLayout(this);
        panel.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
        panel.setPadding(padding, padding, padding, padding);
        panel.setBackgroundDrawable(new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, 
                new int[]{Color.WHITE, Color.GRAY}));
        setContentView(panel); 

        final String path = Environment.getExternalStorageDirectory().getPath();

        // copy images from assets to sdcard
        AppUtils.AssetFileCopy(this, path +"/plasma1.png", "plasma1.png", false);
        AppUtils.AssetFileCopy(this, path +"/plasma2.png", "plasma2.png", false);
        AppUtils.AssetFileCopy(this, path +"/plasma3.png", "plasma3.png", false);
        AppUtils.AssetFileCopy(this, path +"/plasma4.png", "plasma4.png", false);

        //Create carousel view documents
        ArrayList<CarouselDataItem> Docus = new ArrayList<CarouselDataItem>();
        for (int i=0;i<1000;i++) {
            CarouselDataItem docu;
            if (i%4==0) docu = new CarouselDataItem(path +"/plasma1.png", 0, "First Image ");
            else if (i%4==1) docu = new CarouselDataItem(path +"/plasma2.png", 0, "Second Image "+i);
            else if (i%4==2) docu = new CarouselDataItem(path+"/plasma3.png", 0, "Third Image "+i);
            else docu = new CarouselDataItem(path+"/plasma4.png", 0, "4th Image "+i);
            Docus.add(docu);
        } 

        // add the serach filter
        EditText etSearch = new EditText(this);
        etSearch.setHint("Search your documents");
        etSearch.setSingleLine();
        etSearch.setTextColor(Color.BLACK);
        etSearch.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_menu_search, 0, 0, 0);
        AppUtils.AddView(panel, etSearch, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 
                new int[][]{new int[]{RelativeLayout.CENTER_HORIZONTAL}, new int[]{RelativeLayout.ALIGN_PARENT_TOP}}, -1,-1);
        etSearch.addTextChangedListener((TextWatcher) this); 

        // add logo
        TextView tv = new TextView(this);
        tv.setTextColor(Color.BLACK);
        tv.setText("www.pocketmagic.net");
        AppUtils.AddView(panel, tv, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 
                new int[][]{new int[]{RelativeLayout.CENTER_HORIZONTAL}, new int[]{RelativeLayout.ALIGN_PARENT_BOTTOM}}, -1,-1);

        // create the carousel
        CarouselView coverFlow = new CarouselView(this);

        // create adapter and specify device independent items size (scaling)
        // for more details see: http://www.pocketmagic.net/2013/04/how-to-scale-an-android-ui-on-multiple-screens/
        m_carouselAdapter =  new CarouselViewAdapter(this,Docus, m_Inst.Scale(400),m_Inst.Scale(300));
        coverFlow.setAdapter(m_carouselAdapter);
        coverFlow.setSpacing(-1*m_Inst.Scale(150));
        coverFlow.setSelection(Integer.MAX_VALUE / 2, true);
        coverFlow.setAnimationDuration(1000);
        coverFlow.setOnItemSelectedListener((OnItemSelectedListener) this);

        AppUtils.AddView(panel, coverFlow, LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT, 
                new int[][]{new int[]{RelativeLayout.CENTER_IN_PARENT}},
                -1, -1); 
    }

    public void afterTextChanged(Editable arg0) {}

    public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

    public void onTextChanged(CharSequence s, int start, int before, int count) {
        m_carouselAdapter.getFilter().filter(s.toString()); 
    }

    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
         CarouselDataItem docu =  (CarouselDataItem) m_carouselAdapter.getItem((int) arg3);
         if (docu!=null)
             Toast.makeText(this, "You've clicked on:"+docu.getDocText(), Toast.LENGTH_SHORT).show();
    }

    public void onNothingSelected(AdapterView<?> arg0) {}



}

我认真地需要帮助。

1 个答案:

答案 0 :(得分:0)

我用我在AppUtils.java中创建的代码解决了它:

static public void copyFileFromAssets(Context context, String file,
            String dest) throws Exception {
        InputStream in = null;
        OutputStream fout = null;
        int count = 0;

        try {
            in = context.getAssets().open(file);
            fout = new FileOutputStream(new File(dest));

            byte data[] = new byte[1024];
            while ((count = in.read(data, 0, 1024)) != -1) {
                fout.write(data, 0, count);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                }
            }
            if (fout != null) {
                try {
                    fout.close();
                } catch (IOException e) {
                }
            }
        }
    }

我用这个替换了前者,它将文件从assets文件夹中拉到了sdcard