2012-04-09 44 views
2

在我的一個應用程序中,我需要從數據庫中讀取文本並將其顯示給用戶。我想過使用Coverflow來達到這個目的。那麼任何人都可以讓我知道,是否可以使用CoverFlow而不是圖像顯示文本?我正在嘗試生成與this類似的輸出。如所建議的那樣,我將文本轉換爲位圖圖像並嘗試顯示它。但我得到了空白屏幕。請看我的代碼如下Android CoverFlow顯示文字而不是圖像

public class CoverFlowDemoActivity extends Activity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     CoverFlow coverFlow; 
     coverFlow = new CoverFlow(this); 

     coverFlow.setAdapter(new ImageAdapter(this)); 

     ImageAdapter coverImageAdapter = new ImageAdapter(this); 

     coverFlow.setAdapter(coverImageAdapter); 

     coverImageAdapter.populateBitmapArray(); 

     coverFlow.setSpacing(-25); 
     coverFlow.setSelection(4, true); 
     coverFlow.setAnimationDuration(1000); 

     setContentView(coverFlow); 
    } 

    public class ImageAdapter extends BaseAdapter { 
     int mGalleryItemBackground; 
     private Context mContext; 

     private Bitmap[] bitmapImages; 

     public ImageAdapter(Context c) { 
      mContext = c; 
      bitmapImages = new Bitmap[9]; 
     } 

     public void populateBitmapArray() { 

      for (int i = 0; i < 9; i++) { 
       Bitmap originalImage = textAsBitmap("Example Test", 50f, 
         R.color.red); 
       bitmapImages[i] = originalImage; 
      } 

     } 

     public int getCount() { 
      return 9; 
     } 

     public Object getItem(int position) { 
      return position; 
     } 

     public long getItemId(int position) { 
      return position; 
     } 

     public View getView(int position, View convertView, ViewGroup parent) { 

      // Use this code if you want to load from resources 
      ImageView i = new ImageView(mContext); 
      i.setImageBitmap(bitmapImages[position]); 
      i.setLayoutParams(new CoverFlow.LayoutParams(130, 130)); 
      i.setScaleType(ImageView.ScaleType.CENTER_INSIDE); 

      // Make sure we set anti-aliasing otherwise we get jaggies 
      BitmapDrawable drawable = (BitmapDrawable) i.getDrawable(); 
      drawable.setAntiAlias(true); 

      return i; 

      // return mImages[position]; 
     } 

     /** 
     * Returns the size (0.0f to 1.0f) of the views depending on the 
     * 'offset' to the center. 
     */ 
     public float getScale(boolean focused, int offset) { 
      /* Formula: 1/(2^offset) */ 
      return Math.max(0, 1.0f/(float) Math.pow(2, Math.abs(offset))); 
     } 

     public Bitmap textAsBitmap(String text, float largest, int textColor) { 
      Paint paint = new Paint(); 
      paint.setTextSize(largest); 
      paint.setColor(textColor); 
      // int width = (int) (paint.measureText(text) + 0.5f); // round 

      int width = 500; 

      float baseline = (int) (paint.ascent() + 0.5f) + 3f; 
      // int height = (int) ((baseline + paint.descent() + 0.5f) + 3); 

      int height = 500; 
      Bitmap image = Bitmap.createBitmap(width, height, 
        Bitmap.Config.ARGB_8888); 
      Canvas canvas = new Canvas(image); 
      canvas.drawText(text, 0, baseline, paint); 
      return image; 

     } 

    } 
} 
+0

你是在說關於畫廊窗口小部件?如果是這樣,你可以很容易地將你的文本繪製成一個位圖,並顯示它。 (看看Canvas.drawText()) – Renard 2012-04-09 19:02:28

+0

我想你是在討論類似[THIS](http://code.google.com/p/android-coverflow/)的內容,但是你應該清楚你的OP而不是讓我們猜測。有些人不知道'CoverFlow'小部件是什麼。 – adneal 2012-04-09 19:52:08

+0

@aneal是的。我正在嘗試製作與該視頻類似的輸出。 – Allwyn 2012-04-10 04:05:45

回答

1

我一直在做類似於你的事情。而且我意識到你正在你的形象之外繪畫。

要解決此問題,只需將「基準」更改爲「250」,您將看到文字爲圖像。

例如爲:

CHANGE

canvas.drawText(text, 0, baseline, paint); 

TO

canvas.drawText(text, 0, 250, paint); 

希望這將有助於你

+0

謝謝。讓我試着讓你知道它是否有效 – Allwyn 2013-01-28 16:01:41

+0

當然。如果您遇到任何問題,請告訴我。因爲我曾經試過你的代碼,並且在編輯之後就可以工作了^鄰 – 2013-01-31 08:49:11

相關問題