我一直在設計一個需要在自定義視圖上使用滾動條的應用程序。我碰到的問題是,當用戶添加新項目時,我的自定義視圖的高度會不斷變化。我懷疑這不是一個非常不尋常的問題,但使事情複雜化,我實際上並沒有將新對象添加到自定義視圖中,而只是在畫布上繪製位圖圖像。這對我來說很麻煩的原因是,當Android擴大布局時,它將大小設置爲我所說的(0dip),並且在添加位圖時,它只是更改大小以填充滾動視圖;從而每當自定義視圖繪製超出當前高度約束的圖像而不是滾動時,應用程序就會簡單地切斷圖片以保持當前高度。自定義視圖滾動問題Android
(這是一個紙牌遊戲的應用程序,所以我想扇出您當前卡片在一個易於查看和編輯的方式。)
下面是相關代碼:
public class DeckEdit extends Activity implements FilterQueryProvider {
static final int PROGRESS_DIALOG = 0;
private Deck deck;
private CardListAdapter lstAdpt;
private ArrayList<CardImage> cards;
private ProgressDialog progressDialog;
private ProgressThread progressThread;
private String[] cardList;
private DeckEditCardView deckGrid;
private ScrollView sc;
protected Cursor cursor;
private EditText filterText = null;
ArrayAdapter<String> adapter = null;
public void onCreate(Bundle savedInstanceBundle){
super.onCreate(savedInstanceBundle);
deck = new Deck();
//This is what stores the Card Images that I draw to the canvas with
cards = new ArrayList<CardImage>();
setContentView(R.layout.deck_edit_layout);
sc = (ScrollView) findViewById(R.id.deck_scroller);
deckGrid = (DeckEditCardView) findViewById(R.id.deck_grid);
filterText = (EditText) findViewById(R.id.card_search_box);
filterText.addTextChangedListener(filterTextWatcher);
pickDeck();
}
而且那麼這個被稱爲後我加載甲板
private void finishSetup(){
//this is where I set up the deckGrid to have all the cards and then
deckGrid.setCards(cards);
//draw them to the screen
deckGrid.invalidate();
//This is where I realized that the height value might be causing the problem
Log.d("finishSetup()", "deckGrid width: "+deckGrid.getWidth());
Log.d("finishSetup()", "deckGrid height: "+deckGrid.getHeight());
}
的DeckEditCardView類包含此
public class DeckEditCardView extends View implements OnTouchListener {
private ArrayList<CardImage> cards;
private int X_COLS;
@SuppressWarnings("unused")
private int Y_ROWS;
private final int COL_WIDTH;
private final int ROW_HEIGHT;
private Context context;
private final int X_PADDING = 2;
private final int Y_PADDING = 2;
private final int X_CARD_OVERLAY = 7;
private final int Y_CARD_OVERLAY = 5;
public DeckEditCardView(Context context) {
super(context);
cards = new ArrayList<CardImage>();
int cardWidth = getResources().getDrawable(R.drawable.back).getIntrinsicWidth();
int cardHeight = getResources().getDrawable(R.drawable.back).getIntrinsicHeight();
COL_WIDTH = (X_PADDING+(X_CARD_OVERLAY*3)+cardWidth);
Log.d("DECV","COLWIDTH 1: "+COL_WIDTH);
ROW_HEIGHT = (Y_PADDING+(Y_CARD_OVERLAY*3)+cardHeight);
Log.d("DECV","ROWE_HEIGHT 1: "+ROW_HEIGHT);
}
public DeckEditCardView(Context context, AttributeSet attrs) {
super(context, attrs);
cards = new ArrayList<CardImage>();
int cardWidth = getResources().getDrawable(R.drawable.back).getIntrinsicWidth();
int cardHeight = getResources().getDrawable(R.drawable.back).getIntrinsicHeight();
COL_WIDTH = (X_PADDING+(X_CARD_OVERLAY*3)+cardWidth);
Log.d("DECV","COLWIDTH 2 : "+COL_WIDTH);
ROW_HEIGHT = (Y_PADDING+(Y_CARD_OVERLAY*3)+cardHeight);
Log.d("DECV","ROWE_HEIGHT 2: "+ROW_HEIGHT);
}
/*
public DeckEditCardView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
cards = new ArrayList<CardImage>();
int width = getWidth();
int height = getHeight();
int cardWidth = getResources().getDrawable(R.drawable.back).getIntrinsicWidth();
int cardHeight = getResources().getDrawable(R.drawable.back).getIntrinsicHeight();
COL_WIDTH = (X_PADDING+(X_CARD_OVERLAY*3)+cardWidth);
Log.d("DECV","COLWIDTH 3: "+COL_WIDTH);
ROW_HEIGHT = (Y_PADDING+(Y_CARD_OVERLAY*3)+cardHeight);
Log.d("DECV","ROWE_HEIGHT 3: "+ROW_HEIGHT);
X_COLS = (width/COL_WIDTH);
Y_ROWS = (height/ROW_HEIGHT);
}
*/
@Override
public void onFinishInflate(){
super.onFinishInflate();
//this.
}
public void onSizeChanged(int w, int h, int oldw, int oldh){
int width = getWidth();
int height = getHeight();
X_COLS = (width/COL_WIDTH);
Y_ROWS = (height/ROW_HEIGHT);
}
public Point getNextCardPoint(Point curP, boolean sameCard){
Point p=null;
if (sameCard)
p = new Point(curP.x+X_CARD_OVERLAY,curP.y+Y_CARD_OVERLAY);
else { //need to be on the same column
if (point2Col(curP)+1<X_COLS)
p = new Point(COL_WIDTH*(point2Col(curP)+1)+X_PADDING,
ROW_HEIGHT*(point2Row(curP))+Y_PADDING);
else {//add to new column
if (point2Row(curP)+1 > Y_ROWS)
Y_ROWS++;
p = new Point(X_PADDING,
ROW_HEIGHT*(point2Row(curP)+1)+Y_PADDING);
}
}
return p;
}
public void setCards(ArrayList<CardImage> cards){
this.cards = cards;
}
private int point2Col(Point p){
return p.x/COL_WIDTH;
}
private int point2Row(Point p){
return p.y/ROW_HEIGHT;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//ScrollView s = new ScrollView(context);
//s.addView(canvas);
int length = cards.size();
CardImage c;
for (int i = 0; i < length; i++) {
//Log.d("onDraw", "output a card");
c = cards.get(i);
canvas.drawBitmap(c.getBitmap(), c.getX(), c.getY(), null);
}
}
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
return false;
}
}
所以最後,我不知道如何讓用戶滾動並看到不適合在屏幕上的牌(它幾乎可以保證一個完整的甲板將採用滾動條來查看所有的牌。在大多數與此代碼它會讓我在需要的所有卡的圖像,但去屏幕的我不知道那些做什麼:/
而且最後我Layout.xml文件
<com.xeroxchange.ydd.DeckEditCardView android:layout_weight="2"
android:id="@+id/deck_grid"
android:layout_width="fill_parent"
android:layout_height="0dip"></com.xeroxchange.ydd.DeckEditCardView>
</ScrollView>
<LinearLayout android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- Pretty hint text, and maxLines -->
<EditText android:id="@+id/card_search_box"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="type to filter"
android:inputType="text"
android:maxLines="1"/>
<!-- Set height to 0, and let the weight param expand it -->
<!-- Note the use of the default ID! This lets us use a
ListActivity still! -->
<ListView android:id="@+id/card_list"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="2"
/>
</LinearLayout>
</LinearLayout>